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 | |
465ae641 | 137 | if (dev->archdata.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 | ||
172 | dev->archdata.acpi_handle = handle; | |
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 | ||
465ae641 | 201 | if (!dev->archdata.acpi_handle) |
4e10d12a | 202 | return 0; |
1071695f | 203 | |
1033f904 LT |
204 | status = acpi_bus_get_device(dev->archdata.acpi_handle, |
205 | &acpi_dev); | |
206 | if (ACPI_FAILURE(status)) | |
207 | goto err; | |
1071695f | 208 | |
1033f904 LT |
209 | mutex_lock(&acpi_dev->physical_node_lock); |
210 | list_for_each_safe(node, next, &acpi_dev->physical_node_list) { | |
211 | char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2]; | |
212 | ||
213 | entry = list_entry(node, struct acpi_device_physical_node, | |
214 | node); | |
215 | if (entry->dev != dev) | |
216 | continue; | |
217 | ||
218 | list_del(node); | |
219 | clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap); | |
1071695f | 220 | |
1033f904 LT |
221 | acpi_dev->physical_node_count--; |
222 | ||
223 | if (!entry->node_id) | |
224 | strcpy(physical_node_name, PHYSICAL_NODE_STRING); | |
225 | else | |
226 | sprintf(physical_node_name, | |
227 | "physical_node%d", entry->node_id); | |
228 | ||
229 | sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name); | |
230 | sysfs_remove_link(&dev->kobj, "firmware_node"); | |
465ae641 | 231 | dev->archdata.acpi_handle = NULL; |
4e10d12a DSL |
232 | /* acpi_bind_one increase refcnt by one */ |
233 | put_device(dev); | |
1033f904 | 234 | kfree(entry); |
4e10d12a | 235 | } |
1033f904 LT |
236 | mutex_unlock(&acpi_dev->physical_node_lock); |
237 | ||
4e10d12a | 238 | return 0; |
1033f904 LT |
239 | |
240 | err: | |
241 | dev_err(dev, "Oops, 'acpi_handle' corrupt\n"); | |
242 | return -EINVAL; | |
4e10d12a DSL |
243 | } |
244 | ||
245 | static int acpi_platform_notify(struct device *dev) | |
246 | { | |
247 | struct acpi_bus_type *type; | |
248 | acpi_handle handle; | |
249 | int ret = -EINVAL; | |
250 | ||
251 | if (!dev->bus || !dev->parent) { | |
252 | /* bridge devices genernally haven't bus or parent */ | |
253 | ret = acpi_find_bridge_device(dev, &handle); | |
254 | goto end; | |
255 | } | |
256 | type = acpi_get_bus_type(dev->bus); | |
257 | if (!type) { | |
db1461ad | 258 | DBG("No ACPI bus support for %s\n", dev_name(dev)); |
4e10d12a DSL |
259 | ret = -EINVAL; |
260 | goto end; | |
261 | } | |
262 | if ((ret = type->find_device(dev, &handle)) != 0) | |
db1461ad | 263 | DBG("Can't get handler for %s\n", dev_name(dev)); |
4e10d12a DSL |
264 | end: |
265 | if (!ret) | |
266 | acpi_bind_one(dev, handle); | |
267 | ||
268 | #if ACPI_GLUE_DEBUG | |
269 | if (!ret) { | |
270 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
271 | ||
465ae641 BH |
272 | acpi_get_name(dev->archdata.acpi_handle, |
273 | ACPI_FULL_PATHNAME, &buffer); | |
db1461ad | 274 | DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer); |
02438d87 | 275 | kfree(buffer.pointer); |
4e10d12a | 276 | } else |
db1461ad | 277 | DBG("Device %s -> No ACPI support\n", dev_name(dev)); |
4e10d12a DSL |
278 | #endif |
279 | ||
280 | return ret; | |
281 | } | |
282 | ||
283 | static int acpi_platform_notify_remove(struct device *dev) | |
284 | { | |
285 | acpi_unbind_one(dev); | |
286 | return 0; | |
287 | } | |
288 | ||
0e46517d | 289 | int __init init_acpi_device_notify(void) |
4e10d12a | 290 | { |
4e10d12a DSL |
291 | if (platform_notify || platform_notify_remove) { |
292 | printk(KERN_ERR PREFIX "Can't use platform_notify\n"); | |
293 | return 0; | |
294 | } | |
295 | platform_notify = acpi_platform_notify; | |
296 | platform_notify_remove = acpi_platform_notify_remove; | |
297 | return 0; | |
298 | } |