]>
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> | |
d0562674 | 16 | #include <linux/dma-mapping.h> |
4e10d12a | 17 | |
a192a958 LB |
18 | #include "internal.h" |
19 | ||
4e10d12a DSL |
20 | #define ACPI_GLUE_DEBUG 0 |
21 | #if ACPI_GLUE_DEBUG | |
23415eb5 JP |
22 | #define DBG(fmt, ...) \ |
23 | printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__) | |
4e10d12a | 24 | #else |
23415eb5 JP |
25 | #define DBG(fmt, ...) \ |
26 | do { \ | |
27 | if (0) \ | |
28 | printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \ | |
29 | } while (0) | |
4e10d12a DSL |
30 | #endif |
31 | static LIST_HEAD(bus_type_list); | |
32 | static DECLARE_RWSEM(bus_type_sem); | |
33 | ||
1033f904 | 34 | #define PHYSICAL_NODE_STRING "physical_node" |
007ccfcf | 35 | #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10) |
1033f904 | 36 | |
4e10d12a DSL |
37 | int register_acpi_bus_type(struct acpi_bus_type *type) |
38 | { | |
39 | if (acpi_disabled) | |
40 | return -ENODEV; | |
e3f02c52 | 41 | if (type && type->match && type->find_companion) { |
4e10d12a DSL |
42 | down_write(&bus_type_sem); |
43 | list_add_tail(&type->list, &bus_type_list); | |
44 | up_write(&bus_type_sem); | |
53540098 | 45 | printk(KERN_INFO PREFIX "bus type %s registered\n", type->name); |
4e10d12a DSL |
46 | return 0; |
47 | } | |
48 | return -ENODEV; | |
49 | } | |
91e4d5a1 | 50 | EXPORT_SYMBOL_GPL(register_acpi_bus_type); |
4e10d12a | 51 | |
4e10d12a DSL |
52 | int unregister_acpi_bus_type(struct acpi_bus_type *type) |
53 | { | |
54 | if (acpi_disabled) | |
55 | return 0; | |
56 | if (type) { | |
57 | down_write(&bus_type_sem); | |
58 | list_del_init(&type->list); | |
59 | up_write(&bus_type_sem); | |
53540098 RW |
60 | printk(KERN_INFO PREFIX "bus type %s unregistered\n", |
61 | type->name); | |
4e10d12a DSL |
62 | return 0; |
63 | } | |
64 | return -ENODEV; | |
65 | } | |
91e4d5a1 | 66 | EXPORT_SYMBOL_GPL(unregister_acpi_bus_type); |
4e10d12a | 67 | |
53540098 | 68 | static struct acpi_bus_type *acpi_get_bus_type(struct device *dev) |
4e10d12a DSL |
69 | { |
70 | struct acpi_bus_type *tmp, *ret = NULL; | |
71 | ||
72 | down_read(&bus_type_sem); | |
73 | list_for_each_entry(tmp, &bus_type_list, list) { | |
53540098 | 74 | if (tmp->match(dev)) { |
4e10d12a DSL |
75 | ret = tmp; |
76 | break; | |
77 | } | |
78 | } | |
79 | up_read(&bus_type_sem); | |
80 | return ret; | |
81 | } | |
82 | ||
11b88ee2 RW |
83 | #define FIND_CHILD_MIN_SCORE 1 |
84 | #define FIND_CHILD_MAX_SCORE 2 | |
85 | ||
d9fef0c4 | 86 | static int find_child_checks(struct acpi_device *adev, bool check_children) |
60f75b8e | 87 | { |
11b88ee2 | 88 | bool sta_present = true; |
60f75b8e RW |
89 | unsigned long long sta; |
90 | acpi_status status; | |
91 | ||
d9fef0c4 | 92 | status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta); |
11b88ee2 RW |
93 | if (status == AE_NOT_FOUND) |
94 | sta_present = false; | |
95 | else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED)) | |
96 | return -ENODEV; | |
60f75b8e | 97 | |
d9fef0c4 RW |
98 | if (check_children && list_empty(&adev->children)) |
99 | return -ENODEV; | |
60f75b8e | 100 | |
c2a6bbaf RW |
101 | /* |
102 | * If the device has a _HID (or _CID) returning a valid ACPI/PNP | |
103 | * device ID, it is better to make it look less attractive here, so that | |
104 | * the other device with the same _ADR value (that may not have a valid | |
105 | * device ID) can be matched going forward. [This means a second spec | |
106 | * violation in a row, so whatever we do here is best effort anyway.] | |
107 | */ | |
108 | return sta_present && list_empty(&adev->pnp.ids) ? | |
109 | FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE; | |
60f75b8e RW |
110 | } |
111 | ||
d9fef0c4 RW |
112 | struct acpi_device *acpi_find_child_device(struct acpi_device *parent, |
113 | u64 address, bool check_children) | |
60f75b8e | 114 | { |
d9fef0c4 RW |
115 | struct acpi_device *adev, *ret = NULL; |
116 | int ret_score = 0; | |
117 | ||
5ce79d20 RW |
118 | if (!parent) |
119 | return NULL; | |
120 | ||
d9fef0c4 RW |
121 | list_for_each_entry(adev, &parent->children, node) { |
122 | unsigned long long addr; | |
123 | acpi_status status; | |
124 | int score; | |
125 | ||
126 | status = acpi_evaluate_integer(adev->handle, METHOD_NAME__ADR, | |
127 | NULL, &addr); | |
128 | if (ACPI_FAILURE(status) || addr != address) | |
129 | continue; | |
130 | ||
131 | if (!ret) { | |
132 | /* This is the first matching object. Save it. */ | |
133 | ret = adev; | |
134 | continue; | |
135 | } | |
136 | /* | |
137 | * There is more than one matching device object with the same | |
138 | * _ADR value. That really is unexpected, so we are kind of | |
139 | * beyond the scope of the spec here. We have to choose which | |
140 | * one to return, though. | |
141 | * | |
142 | * First, check if the previously found object is good enough | |
143 | * and return it if so. Second, do the same for the object that | |
144 | * we've just found. | |
145 | */ | |
146 | if (!ret_score) { | |
147 | ret_score = find_child_checks(ret, check_children); | |
148 | if (ret_score == FIND_CHILD_MAX_SCORE) | |
149 | return ret; | |
150 | } | |
151 | score = find_child_checks(adev, check_children); | |
152 | if (score == FIND_CHILD_MAX_SCORE) { | |
153 | return adev; | |
154 | } else if (score > ret_score) { | |
155 | ret = adev; | |
156 | ret_score = score; | |
157 | } | |
4e10d12a | 158 | } |
d9fef0c4 | 159 | return ret; |
4e10d12a | 160 | } |
9c5ad36d | 161 | EXPORT_SYMBOL_GPL(acpi_find_child_device); |
4e10d12a | 162 | |
bdbdbf91 RW |
163 | static void acpi_physnode_link_name(char *buf, unsigned int node_id) |
164 | { | |
165 | if (node_id > 0) | |
166 | snprintf(buf, PHYSICAL_NODE_NAME_SIZE, | |
167 | PHYSICAL_NODE_STRING "%u", node_id); | |
168 | else | |
169 | strcpy(buf, PHYSICAL_NODE_STRING); | |
170 | } | |
171 | ||
24dee1fc | 172 | int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev) |
4e10d12a | 173 | { |
f3fd0c8a | 174 | struct acpi_device_physical_node *physical_node, *pn; |
007ccfcf RW |
175 | char physical_node_name[PHYSICAL_NODE_NAME_SIZE]; |
176 | struct list_head *physnode_list; | |
177 | unsigned int node_id; | |
1033f904 | 178 | int retval = -EINVAL; |
1831eff8 | 179 | enum dev_dma_attr attr; |
4e10d12a | 180 | |
ca5b74d2 | 181 | if (has_acpi_companion(dev)) { |
24dee1fc | 182 | if (acpi_dev) { |
7b199811 | 183 | dev_warn(dev, "ACPI companion already set\n"); |
f3fd0c8a RW |
184 | return -EINVAL; |
185 | } else { | |
7b199811 | 186 | acpi_dev = ACPI_COMPANION(dev); |
f3fd0c8a | 187 | } |
4e10d12a | 188 | } |
7b199811 | 189 | if (!acpi_dev) |
f3fd0c8a | 190 | return -EINVAL; |
1033f904 | 191 | |
a104b4d4 | 192 | get_device(&acpi_dev->dev); |
4e10d12a | 193 | get_device(dev); |
f3fd0c8a | 194 | physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL); |
1033f904 LT |
195 | if (!physical_node) { |
196 | retval = -ENOMEM; | |
197 | goto err; | |
4e10d12a | 198 | } |
4e10d12a | 199 | |
1033f904 | 200 | mutex_lock(&acpi_dev->physical_node_lock); |
f3fd0c8a | 201 | |
007ccfcf RW |
202 | /* |
203 | * Keep the list sorted by node_id so that the IDs of removed nodes can | |
204 | * be recycled easily. | |
205 | */ | |
206 | physnode_list = &acpi_dev->physical_node_list; | |
207 | node_id = 0; | |
208 | list_for_each_entry(pn, &acpi_dev->physical_node_list, node) { | |
209 | /* Sanity check. */ | |
f3fd0c8a | 210 | if (pn->dev == dev) { |
3342c753 RW |
211 | mutex_unlock(&acpi_dev->physical_node_lock); |
212 | ||
f3fd0c8a | 213 | dev_warn(dev, "Already associated with ACPI node\n"); |
3342c753 | 214 | kfree(physical_node); |
7b199811 | 215 | if (ACPI_COMPANION(dev) != acpi_dev) |
3342c753 | 216 | goto err; |
3fe444ad | 217 | |
3342c753 | 218 | put_device(dev); |
a104b4d4 | 219 | put_device(&acpi_dev->dev); |
3342c753 | 220 | return 0; |
f3fd0c8a | 221 | } |
007ccfcf RW |
222 | if (pn->node_id == node_id) { |
223 | physnode_list = &pn->node; | |
224 | node_id++; | |
225 | } | |
1071695f DB |
226 | } |
227 | ||
007ccfcf | 228 | physical_node->node_id = node_id; |
1033f904 | 229 | physical_node->dev = dev; |
007ccfcf | 230 | list_add(&physical_node->node, physnode_list); |
1033f904 | 231 | acpi_dev->physical_node_count++; |
f3fd0c8a | 232 | |
ca5b74d2 | 233 | if (!has_acpi_companion(dev)) |
7b199811 | 234 | ACPI_COMPANION_SET(dev, acpi_dev); |
1033f904 | 235 | |
1831eff8 SS |
236 | attr = acpi_get_dma_attr(acpi_dev); |
237 | if (attr != DEV_DMA_NOT_SUPPORTED) | |
d760a1ba | 238 | acpi_dma_configure(dev, attr); |
d0562674 | 239 | |
bdbdbf91 | 240 | acpi_physnode_link_name(physical_node_name, node_id); |
1033f904 | 241 | retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, |
f501b6ec | 242 | physical_node_name); |
464c1147 RW |
243 | if (retval) |
244 | dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n", | |
245 | physical_node_name, retval); | |
246 | ||
1033f904 | 247 | retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj, |
f501b6ec | 248 | "firmware_node"); |
464c1147 RW |
249 | if (retval) |
250 | dev_err(dev, "Failed to create link firmware_node (%d)\n", | |
251 | retval); | |
1033f904 | 252 | |
40055206 RW |
253 | mutex_unlock(&acpi_dev->physical_node_lock); |
254 | ||
1033f904 LT |
255 | if (acpi_dev->wakeup.flags.valid) |
256 | device_set_wakeup_capable(dev, true); | |
257 | ||
4e10d12a | 258 | return 0; |
1033f904 LT |
259 | |
260 | err: | |
7b199811 | 261 | ACPI_COMPANION_SET(dev, NULL); |
1033f904 | 262 | put_device(dev); |
a104b4d4 | 263 | put_device(&acpi_dev->dev); |
1033f904 | 264 | return retval; |
4e10d12a | 265 | } |
ac212b69 | 266 | EXPORT_SYMBOL_GPL(acpi_bind_one); |
4e10d12a | 267 | |
ac212b69 | 268 | int acpi_unbind_one(struct device *dev) |
4e10d12a | 269 | { |
7b199811 | 270 | struct acpi_device *acpi_dev = ACPI_COMPANION(dev); |
1033f904 | 271 | struct acpi_device_physical_node *entry; |
1033f904 | 272 | |
7b199811 | 273 | if (!acpi_dev) |
4e10d12a | 274 | return 0; |
1071695f | 275 | |
1033f904 | 276 | mutex_lock(&acpi_dev->physical_node_lock); |
3e332783 RW |
277 | |
278 | list_for_each_entry(entry, &acpi_dev->physical_node_list, node) | |
279 | if (entry->dev == dev) { | |
280 | char physnode_name[PHYSICAL_NODE_NAME_SIZE]; | |
281 | ||
282 | list_del(&entry->node); | |
283 | acpi_dev->physical_node_count--; | |
284 | ||
285 | acpi_physnode_link_name(physnode_name, entry->node_id); | |
286 | sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name); | |
287 | sysfs_remove_link(&dev->kobj, "firmware_node"); | |
7b199811 | 288 | ACPI_COMPANION_SET(dev, NULL); |
a104b4d4 | 289 | /* Drop references taken by acpi_bind_one(). */ |
3e332783 | 290 | put_device(dev); |
a104b4d4 | 291 | put_device(&acpi_dev->dev); |
3e332783 RW |
292 | kfree(entry); |
293 | break; | |
294 | } | |
295 | ||
1033f904 | 296 | mutex_unlock(&acpi_dev->physical_node_lock); |
4e10d12a DSL |
297 | return 0; |
298 | } | |
ac212b69 | 299 | EXPORT_SYMBOL_GPL(acpi_unbind_one); |
4e10d12a DSL |
300 | |
301 | static int acpi_platform_notify(struct device *dev) | |
302 | { | |
53540098 | 303 | struct acpi_bus_type *type = acpi_get_bus_type(dev); |
9cb32acf | 304 | struct acpi_device *adev; |
11909ca1 | 305 | int ret; |
4e10d12a | 306 | |
f3fd0c8a | 307 | ret = acpi_bind_one(dev, NULL); |
92414481 | 308 | if (ret && type) { |
e3f02c52 RW |
309 | struct acpi_device *adev; |
310 | ||
311 | adev = type->find_companion(dev); | |
312 | if (!adev) { | |
11909ca1 | 313 | DBG("Unable to get handle for %s\n", dev_name(dev)); |
e3f02c52 | 314 | ret = -ENODEV; |
11909ca1 RW |
315 | goto out; |
316 | } | |
24dee1fc | 317 | ret = acpi_bind_one(dev, adev); |
11909ca1 RW |
318 | if (ret) |
319 | goto out; | |
4e10d12a | 320 | } |
9cb32acf RW |
321 | adev = ACPI_COMPANION(dev); |
322 | if (!adev) | |
323 | goto out; | |
11909ca1 RW |
324 | |
325 | if (type && type->setup) | |
326 | type->setup(dev); | |
9cb32acf RW |
327 | else if (adev->handler && adev->handler->bind) |
328 | adev->handler->bind(dev); | |
4e10d12a | 329 | |
f3fd0c8a | 330 | out: |
4e10d12a DSL |
331 | #if ACPI_GLUE_DEBUG |
332 | if (!ret) { | |
333 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
334 | ||
a412a11d | 335 | acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer); |
db1461ad | 336 | DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer); |
02438d87 | 337 | kfree(buffer.pointer); |
4e10d12a | 338 | } else |
db1461ad | 339 | DBG("Device %s -> No ACPI support\n", dev_name(dev)); |
4e10d12a DSL |
340 | #endif |
341 | ||
342 | return ret; | |
343 | } | |
344 | ||
345 | static int acpi_platform_notify_remove(struct device *dev) | |
346 | { | |
9cb32acf | 347 | struct acpi_device *adev = ACPI_COMPANION(dev); |
11909ca1 RW |
348 | struct acpi_bus_type *type; |
349 | ||
9cb32acf RW |
350 | if (!adev) |
351 | return 0; | |
352 | ||
53540098 | 353 | type = acpi_get_bus_type(dev); |
11909ca1 RW |
354 | if (type && type->cleanup) |
355 | type->cleanup(dev); | |
9cb32acf RW |
356 | else if (adev->handler && adev->handler->unbind) |
357 | adev->handler->unbind(dev); | |
11909ca1 | 358 | |
4e10d12a DSL |
359 | acpi_unbind_one(dev); |
360 | return 0; | |
361 | } | |
362 | ||
c33cab60 | 363 | void __init init_acpi_device_notify(void) |
4e10d12a | 364 | { |
4e10d12a DSL |
365 | if (platform_notify || platform_notify_remove) { |
366 | printk(KERN_ERR PREFIX "Can't use platform_notify\n"); | |
c33cab60 | 367 | return; |
4e10d12a DSL |
368 | } |
369 | platform_notify = acpi_platform_notify; | |
370 | platform_notify_remove = acpi_platform_notify_remove; | |
4e10d12a | 371 | } |