]>
Commit | Line | Data |
---|---|---|
4e10d12a DSL |
1 | /* |
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 | */ | |
214f2c90 | 9 | #include <linux/export.h> |
4e10d12a DSL |
10 | #include <linux/init.h> |
11 | #include <linux/list.h> | |
12 | #include <linux/device.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
4e10d12a DSL |
14 | #include <linux/rwsem.h> |
15 | #include <linux/acpi.h> | |
16 | ||
a192a958 LB |
17 | #include "internal.h" |
18 | ||
4e10d12a DSL |
19 | #define ACPI_GLUE_DEBUG 0 |
20 | #if ACPI_GLUE_DEBUG | |
21 | #define DBG(x...) printk(PREFIX x) | |
22 | #else | |
4ebf83c8 | 23 | #define DBG(x...) do { } while(0) |
4e10d12a DSL |
24 | #endif |
25 | static LIST_HEAD(bus_type_list); | |
26 | static DECLARE_RWSEM(bus_type_sem); | |
27 | ||
1033f904 LT |
28 | #define PHYSICAL_NODE_STRING "physical_node" |
29 | ||
4e10d12a DSL |
30 | int 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); | |
4be44fcd LB |
38 | printk(KERN_INFO PREFIX "bus type %s registered\n", |
39 | type->bus->name); | |
4e10d12a DSL |
40 | return 0; |
41 | } | |
42 | return -ENODEV; | |
43 | } | |
91e4d5a1 | 44 | EXPORT_SYMBOL_GPL(register_acpi_bus_type); |
4e10d12a | 45 | |
4e10d12a DSL |
46 | int 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); | |
4be44fcd LB |
54 | printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n", |
55 | type->bus->name); | |
4e10d12a DSL |
56 | return 0; |
57 | } | |
58 | return -ENODEV; | |
59 | } | |
91e4d5a1 | 60 | EXPORT_SYMBOL_GPL(unregister_acpi_bus_type); |
4e10d12a | 61 | |
4e10d12a DSL |
62 | static 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 | ||
77 | static 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 | ||
4e10d12a DSL |
93 | /* Get device's handler per its address under its parent */ |
94 | struct acpi_find_child { | |
95 | acpi_handle handle; | |
439913ff | 96 | u64 address; |
4e10d12a DSL |
97 | }; |
98 | ||
99 | static acpi_status | |
100 | do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv) | |
101 | { | |
102 | acpi_status status; | |
103 | struct acpi_device_info *info; | |
50dd0969 | 104 | struct acpi_find_child *find = context; |
4e10d12a | 105 | |
15b8dd53 | 106 | status = acpi_get_object_info(handle, &info); |
4e10d12a | 107 | if (ACPI_SUCCESS(status)) { |
108029ff ZY |
108 | if ((info->address == find->address) |
109 | && (info->valid & ACPI_VALID_ADR)) | |
4e10d12a | 110 | find->handle = handle; |
15b8dd53 | 111 | kfree(info); |
4e10d12a DSL |
112 | } |
113 | return AE_OK; | |
114 | } | |
115 | ||
439913ff | 116 | acpi_handle acpi_get_child(acpi_handle parent, u64 address) |
4e10d12a DSL |
117 | { |
118 | struct acpi_find_child find = { NULL, address }; | |
119 | ||
120 | if (!parent) | |
121 | return NULL; | |
122 | acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, | |
2263576c | 123 | 1, do_acpi_find_child, NULL, &find, NULL); |
4e10d12a DSL |
124 | return find.handle; |
125 | } | |
126 | ||
127 | EXPORT_SYMBOL(acpi_get_child); | |
128 | ||
4e10d12a DSL |
129 | static int acpi_bind_one(struct device *dev, acpi_handle handle) |
130 | { | |
1071695f | 131 | struct acpi_device *acpi_dev; |
4e10d12a | 132 | acpi_status status; |
1033f904 LT |
133 | struct acpi_device_physical_node *physical_node; |
134 | char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2]; | |
135 | int retval = -EINVAL; | |
4e10d12a | 136 | |
06f64c8f | 137 | if (dev->acpi_handle) { |
fc3a8828 | 138 | dev_warn(dev, "Drivers changed 'acpi_handle'\n"); |
4e10d12a DSL |
139 | return -EINVAL; |
140 | } | |
1033f904 | 141 | |
4e10d12a | 142 | get_device(dev); |
1033f904 LT |
143 | 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; | |
4e10d12a | 152 | } |
4e10d12a | 153 | |
1033f904 LT |
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); | |
2978af54 | 162 | kfree(physical_node); |
1033f904 | 163 | goto err; |
1071695f DB |
164 | } |
165 | ||
1033f904 LT |
166 | set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap); |
167 | physical_node->dev = dev; | |
168 | list_add_tail(&physical_node->node, &acpi_dev->physical_node_list); | |
169 | acpi_dev->physical_node_count++; | |
170 | mutex_unlock(&acpi_dev->physical_node_lock); | |
171 | ||
06f64c8f | 172 | dev->acpi_handle = handle; |
1033f904 LT |
173 | |
174 | if (!physical_node->node_id) | |
175 | strcpy(physical_node_name, PHYSICAL_NODE_STRING); | |
176 | else | |
177 | sprintf(physical_node_name, | |
178 | "physical_node%d", physical_node->node_id); | |
179 | retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, | |
180 | physical_node_name); | |
181 | retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj, | |
182 | "firmware_node"); | |
183 | ||
184 | if (acpi_dev->wakeup.flags.valid) | |
185 | device_set_wakeup_capable(dev, true); | |
186 | ||
4e10d12a | 187 | return 0; |
1033f904 LT |
188 | |
189 | err: | |
190 | put_device(dev); | |
191 | return retval; | |
4e10d12a DSL |
192 | } |
193 | ||
194 | static int acpi_unbind_one(struct device *dev) | |
195 | { | |
1033f904 LT |
196 | struct acpi_device_physical_node *entry; |
197 | struct acpi_device *acpi_dev; | |
198 | acpi_status status; | |
199 | struct list_head *node, *next; | |
200 | ||
06f64c8f | 201 | if (!dev->acpi_handle) |
4e10d12a | 202 | return 0; |
1071695f | 203 | |
06f64c8f | 204 | status = acpi_bus_get_device(dev->acpi_handle, &acpi_dev); |
1033f904 LT |
205 | if (ACPI_FAILURE(status)) |
206 | goto err; | |
1071695f | 207 | |
1033f904 LT |
208 | 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]; | |
211 | ||
212 | 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); | |
1071695f | 219 | |
1033f904 LT |
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"); | |
06f64c8f | 230 | dev->acpi_handle = NULL; |
4e10d12a DSL |
231 | /* acpi_bind_one increase refcnt by one */ |
232 | put_device(dev); | |
1033f904 | 233 | kfree(entry); |
4e10d12a | 234 | } |
1033f904 LT |
235 | mutex_unlock(&acpi_dev->physical_node_lock); |
236 | ||
4e10d12a | 237 | return 0; |
1033f904 LT |
238 | |
239 | err: | |
240 | dev_err(dev, "Oops, 'acpi_handle' corrupt\n"); | |
241 | return -EINVAL; | |
4e10d12a DSL |
242 | } |
243 | ||
244 | static 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) { | |
db1461ad | 257 | DBG("No ACPI bus support for %s\n", dev_name(dev)); |
4e10d12a DSL |
258 | ret = -EINVAL; |
259 | goto end; | |
260 | } | |
261 | if ((ret = type->find_device(dev, &handle)) != 0) | |
db1461ad | 262 | DBG("Can't get handler for %s\n", dev_name(dev)); |
4e10d12a DSL |
263 | 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 | ||
06f64c8f | 271 | acpi_get_name(dev->acpi_handle, ACPI_FULL_PATHNAME, &buffer); |
db1461ad | 272 | DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer); |
02438d87 | 273 | kfree(buffer.pointer); |
4e10d12a | 274 | } else |
db1461ad | 275 | DBG("Device %s -> No ACPI support\n", dev_name(dev)); |
4e10d12a DSL |
276 | #endif |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
281 | static int acpi_platform_notify_remove(struct device *dev) | |
282 | { | |
283 | acpi_unbind_one(dev); | |
284 | return 0; | |
285 | } | |
286 | ||
0e46517d | 287 | int __init init_acpi_device_notify(void) |
4e10d12a | 288 | { |
4e10d12a DSL |
289 | if (platform_notify || platform_notify_remove) { |
290 | printk(KERN_ERR PREFIX "Can't use platform_notify\n"); | |
291 | return 0; | |
292 | } | |
293 | platform_notify = acpi_platform_notify; | |
294 | platform_notify_remove = acpi_platform_notify_remove; | |
295 | return 0; | |
296 | } |