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.
9 #include <linux/export.h>
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/rwsem.h>
15 #include <linux/acpi.h>
19 #define ACPI_GLUE_DEBUG 0
21 #define DBG(fmt, ...) \
22 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
24 #define DBG(fmt, ...) \
27 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
30 static LIST_HEAD(bus_type_list
);
31 static DECLARE_RWSEM(bus_type_sem
);
33 #define PHYSICAL_NODE_STRING "physical_node"
35 int register_acpi_bus_type(struct acpi_bus_type
*type
)
39 if (type
&& type
->match
&& type
->find_device
) {
40 down_write(&bus_type_sem
);
41 list_add_tail(&type
->list
, &bus_type_list
);
42 up_write(&bus_type_sem
);
43 printk(KERN_INFO PREFIX
"bus type %s registered\n", type
->name
);
48 EXPORT_SYMBOL_GPL(register_acpi_bus_type
);
50 int unregister_acpi_bus_type(struct acpi_bus_type
*type
)
55 down_write(&bus_type_sem
);
56 list_del_init(&type
->list
);
57 up_write(&bus_type_sem
);
58 printk(KERN_INFO PREFIX
"bus type %s unregistered\n",
64 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type
);
66 static struct acpi_bus_type
*acpi_get_bus_type(struct device
*dev
)
68 struct acpi_bus_type
*tmp
, *ret
= NULL
;
70 down_read(&bus_type_sem
);
71 list_for_each_entry(tmp
, &bus_type_list
, list
) {
72 if (tmp
->match(dev
)) {
77 up_read(&bus_type_sem
);
81 static acpi_status
do_acpi_find_child(acpi_handle handle
, u32 lvl_not_used
,
82 void *addr_p
, void **ret_p
)
84 unsigned long long addr
;
87 status
= acpi_evaluate_integer(handle
, METHOD_NAME__ADR
, NULL
, &addr
);
88 if (ACPI_SUCCESS(status
) && addr
== *((u64
*)addr_p
)) {
90 return AE_CTRL_TERMINATE
;
95 acpi_handle
acpi_get_child(acpi_handle parent
, u64 address
)
102 acpi_walk_namespace(ACPI_TYPE_DEVICE
, parent
, 1, NULL
,
103 do_acpi_find_child
, &address
, &ret
);
104 return (acpi_handle
)ret
;
106 EXPORT_SYMBOL(acpi_get_child
);
108 static int acpi_bind_one(struct device
*dev
, acpi_handle handle
)
110 struct acpi_device
*acpi_dev
;
112 struct acpi_device_physical_node
*physical_node
, *pn
;
113 char physical_node_name
[sizeof(PHYSICAL_NODE_STRING
) + 2];
114 int retval
= -EINVAL
;
116 if (ACPI_HANDLE(dev
)) {
118 dev_warn(dev
, "ACPI handle is already set\n");
121 handle
= ACPI_HANDLE(dev
);
128 status
= acpi_bus_get_device(handle
, &acpi_dev
);
129 if (ACPI_FAILURE(status
))
132 physical_node
= kzalloc(sizeof(*physical_node
), GFP_KERNEL
);
133 if (!physical_node
) {
138 mutex_lock(&acpi_dev
->physical_node_lock
);
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");
147 /* 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
) {
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
++;
161 mutex_unlock(&acpi_dev
->physical_node_lock
);
163 if (!ACPI_HANDLE(dev
))
164 ACPI_HANDLE_SET(dev
, acpi_dev
->handle
);
166 if (!physical_node
->node_id
)
167 strcpy(physical_node_name
, PHYSICAL_NODE_STRING
);
169 sprintf(physical_node_name
,
170 "physical_node%d", physical_node
->node_id
);
171 retval
= sysfs_create_link(&acpi_dev
->dev
.kobj
, &dev
->kobj
,
173 retval
= sysfs_create_link(&dev
->kobj
, &acpi_dev
->dev
.kobj
,
176 if (acpi_dev
->wakeup
.flags
.valid
)
177 device_set_wakeup_capable(dev
, true);
182 ACPI_HANDLE_SET(dev
, NULL
);
187 mutex_unlock(&acpi_dev
->physical_node_lock
);
188 kfree(physical_node
);
192 static int acpi_unbind_one(struct device
*dev
)
194 struct acpi_device_physical_node
*entry
;
195 struct acpi_device
*acpi_dev
;
197 struct list_head
*node
, *next
;
199 if (!ACPI_HANDLE(dev
))
202 status
= acpi_bus_get_device(ACPI_HANDLE(dev
), &acpi_dev
);
203 if (ACPI_FAILURE(status
))
206 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];
210 entry
= list_entry(node
, struct acpi_device_physical_node
,
212 if (entry
->dev
!= dev
)
216 clear_bit(entry
->node_id
, acpi_dev
->physical_node_id_bitmap
);
218 acpi_dev
->physical_node_count
--;
221 strcpy(physical_node_name
, PHYSICAL_NODE_STRING
);
223 sprintf(physical_node_name
,
224 "physical_node%d", entry
->node_id
);
226 sysfs_remove_link(&acpi_dev
->dev
.kobj
, physical_node_name
);
227 sysfs_remove_link(&dev
->kobj
, "firmware_node");
228 ACPI_HANDLE_SET(dev
, NULL
);
229 /* acpi_bind_one increase refcnt by one */
233 mutex_unlock(&acpi_dev
->physical_node_lock
);
238 dev_err(dev
, "Oops, 'acpi_handle' corrupt\n");
242 static int acpi_platform_notify(struct device
*dev
)
244 struct acpi_bus_type
*type
= acpi_get_bus_type(dev
);
248 ret
= acpi_bind_one(dev
, NULL
);
250 ret
= type
->find_device(dev
, &handle
);
252 DBG("Unable to get handle for %s\n", dev_name(dev
));
255 ret
= acpi_bind_one(dev
, handle
);
260 if (type
&& type
->setup
)
266 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
268 acpi_get_name(ACPI_HANDLE(dev
), ACPI_FULL_PATHNAME
, &buffer
);
269 DBG("Device %s -> %s\n", dev_name(dev
), (char *)buffer
.pointer
);
270 kfree(buffer
.pointer
);
272 DBG("Device %s -> No ACPI support\n", dev_name(dev
));
278 static int acpi_platform_notify_remove(struct device
*dev
)
280 struct acpi_bus_type
*type
;
282 type
= acpi_get_bus_type(dev
);
283 if (type
&& type
->cleanup
)
286 acpi_unbind_one(dev
);
290 int __init
init_acpi_device_notify(void)
292 if (platform_notify
|| platform_notify_remove
) {
293 printk(KERN_ERR PREFIX
"Can't use platform_notify\n");
296 platform_notify
= acpi_platform_notify
;
297 platform_notify_remove
= acpi_platform_notify_remove
;