Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * scan.c - support for transforming the ACPI namespace into individual objects | |
3 | */ | |
4 | ||
5 | #include <linux/module.h> | |
6 | #include <linux/init.h> | |
5a0e3ad6 | 7 | #include <linux/slab.h> |
9b6d97b6 | 8 | #include <linux/kernel.h> |
1da177e4 | 9 | #include <linux/acpi.h> |
74523c90 AK |
10 | #include <linux/signal.h> |
11 | #include <linux/kthread.h> | |
222e82ac | 12 | #include <linux/dmi.h> |
d1efe3c3 | 13 | #include <linux/nls.h> |
1da177e4 | 14 | |
d783156e RW |
15 | #include <asm/pgtable.h> |
16 | ||
e60cc7a6 BH |
17 | #include "internal.h" |
18 | ||
1da177e4 | 19 | #define _COMPONENT ACPI_BUS_COMPONENT |
f52fd66d | 20 | ACPI_MODULE_NAME("scan"); |
4be44fcd | 21 | extern struct acpi_device *acpi_root; |
1da177e4 LT |
22 | |
23 | #define ACPI_BUS_CLASS "system_bus" | |
29b71a1c | 24 | #define ACPI_BUS_HID "LNXSYBUS" |
1da177e4 LT |
25 | #define ACPI_BUS_DEVICE_NAME "System Bus" |
26 | ||
859ac9a4 BH |
27 | #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent) |
28 | ||
d783156e RW |
29 | #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page) |
30 | ||
683058e3 RW |
31 | /* |
32 | * If set, devices will be hot-removed even if they cannot be put offline | |
33 | * gracefully (from the kernel's standpoint). | |
34 | */ | |
35 | bool acpi_force_hot_remove; | |
36 | ||
620e112c | 37 | static const char *dummy_hid = "device"; |
2b2ae7c7 | 38 | |
e49bd2dd | 39 | static LIST_HEAD(acpi_bus_id_list); |
c511cc19 | 40 | static DEFINE_MUTEX(acpi_scan_lock); |
ca589f94 | 41 | static LIST_HEAD(acpi_scan_handlers_list); |
9090589d | 42 | DEFINE_MUTEX(acpi_device_lock); |
1da177e4 LT |
43 | LIST_HEAD(acpi_wakeup_device_list); |
44 | ||
e49bd2dd | 45 | struct acpi_device_bus_id{ |
bb095854 | 46 | char bus_id[15]; |
e49bd2dd ZR |
47 | unsigned int instance_no; |
48 | struct list_head node; | |
1da177e4 | 49 | }; |
29b71a1c | 50 | |
3757b948 RW |
51 | void acpi_scan_lock_acquire(void) |
52 | { | |
53 | mutex_lock(&acpi_scan_lock); | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire); | |
56 | ||
57 | void acpi_scan_lock_release(void) | |
58 | { | |
59 | mutex_unlock(&acpi_scan_lock); | |
60 | } | |
61 | EXPORT_SYMBOL_GPL(acpi_scan_lock_release); | |
62 | ||
ca589f94 RW |
63 | int acpi_scan_add_handler(struct acpi_scan_handler *handler) |
64 | { | |
65 | if (!handler || !handler->attach) | |
66 | return -EINVAL; | |
67 | ||
68 | list_add_tail(&handler->list_node, &acpi_scan_handlers_list); | |
69 | return 0; | |
70 | } | |
71 | ||
3f8055c3 RW |
72 | int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler, |
73 | const char *hotplug_profile_name) | |
74 | { | |
75 | int error; | |
76 | ||
77 | error = acpi_scan_add_handler(handler); | |
78 | if (error) | |
79 | return error; | |
80 | ||
81 | acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name); | |
82 | return 0; | |
83 | } | |
84 | ||
29b71a1c TR |
85 | /* |
86 | * Creates hid/cid(s) string needed for modalias and uevent | |
87 | * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: | |
88 | * char *modalias: "acpi:IBM0001:ACPI0001" | |
3d8e0090 ZR |
89 | * Return: 0: no _HID and no _CID |
90 | * -EINVAL: output error | |
91 | * -ENOMEM: output is truncated | |
29b71a1c | 92 | */ |
b3e572d2 AB |
93 | static int create_modalias(struct acpi_device *acpi_dev, char *modalias, |
94 | int size) | |
95 | { | |
29b71a1c | 96 | int len; |
5c9fcb5d | 97 | int count; |
7f47fa6c | 98 | struct acpi_hardware_id *id; |
29b71a1c | 99 | |
2b2ae7c7 TR |
100 | if (list_empty(&acpi_dev->pnp.ids)) |
101 | return 0; | |
102 | ||
5c9fcb5d | 103 | len = snprintf(modalias, size, "acpi:"); |
29b71a1c TR |
104 | size -= len; |
105 | ||
7f47fa6c BH |
106 | list_for_each_entry(id, &acpi_dev->pnp.ids, list) { |
107 | count = snprintf(&modalias[len], size, "%s:", id->id); | |
3d8e0090 ZR |
108 | if (count < 0) |
109 | return EINVAL; | |
110 | if (count >= size) | |
111 | return -ENOMEM; | |
5c9fcb5d ZR |
112 | len += count; |
113 | size -= count; | |
114 | } | |
115 | ||
29b71a1c TR |
116 | modalias[len] = '\0'; |
117 | return len; | |
118 | } | |
119 | ||
6eb2451f ZR |
120 | /* |
121 | * Creates uevent modalias field for ACPI enumerated devices. | |
122 | * Because the other buses does not support ACPI HIDs & CIDs. | |
123 | * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get: | |
124 | * "acpi:IBM0001:ACPI0001" | |
125 | */ | |
126 | int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) | |
127 | { | |
128 | struct acpi_device *acpi_dev; | |
129 | int len; | |
130 | ||
131 | acpi_dev = ACPI_COMPANION(dev); | |
132 | if (!acpi_dev) | |
133 | return -ENODEV; | |
134 | ||
135 | /* Fall back to bus specific way of modalias exporting */ | |
136 | if (list_empty(&acpi_dev->pnp.ids)) | |
137 | return -ENODEV; | |
138 | ||
139 | if (add_uevent_var(env, "MODALIAS=")) | |
140 | return -ENOMEM; | |
141 | len = create_modalias(acpi_dev, &env->buf[env->buflen - 1], | |
142 | sizeof(env->buf) - env->buflen); | |
143 | if (len <= 0) | |
144 | return len; | |
145 | env->buflen += len; | |
146 | return 0; | |
147 | } | |
148 | EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias); | |
149 | ||
150 | /* | |
151 | * Creates modalias sysfs attribute for ACPI enumerated devices. | |
152 | * Because the other buses does not support ACPI HIDs & CIDs. | |
153 | * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get: | |
154 | * "acpi:IBM0001:ACPI0001" | |
155 | */ | |
156 | int acpi_device_modalias(struct device *dev, char *buf, int size) | |
157 | { | |
158 | struct acpi_device *acpi_dev; | |
159 | int len; | |
160 | ||
161 | acpi_dev = ACPI_COMPANION(dev); | |
162 | if (!acpi_dev) | |
163 | return -ENODEV; | |
164 | ||
165 | /* Fall back to bus specific way of modalias exporting */ | |
166 | if (list_empty(&acpi_dev->pnp.ids)) | |
167 | return -ENODEV; | |
168 | ||
169 | len = create_modalias(acpi_dev, buf, size -1); | |
170 | if (len <= 0) | |
171 | return len; | |
172 | buf[len++] = '\n'; | |
173 | return len; | |
174 | } | |
175 | EXPORT_SYMBOL_GPL(acpi_device_modalias); | |
176 | ||
29b71a1c TR |
177 | static ssize_t |
178 | acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { | |
179 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
180 | int len; | |
181 | ||
29b71a1c TR |
182 | len = create_modalias(acpi_dev, buf, 1024); |
183 | if (len <= 0) | |
3d8e0090 | 184 | return len; |
29b71a1c TR |
185 | buf[len++] = '\n'; |
186 | return len; | |
187 | } | |
188 | static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); | |
189 | ||
caa73ea1 | 190 | bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent) |
d22ddcbc RW |
191 | { |
192 | struct acpi_device_physical_node *pn; | |
193 | bool offline = true; | |
194 | ||
195 | mutex_lock(&adev->physical_node_lock); | |
196 | ||
197 | list_for_each_entry(pn, &adev->physical_node_list, node) | |
198 | if (device_supports_offline(pn->dev) && !pn->dev->offline) { | |
caa73ea1 RW |
199 | if (uevent) |
200 | kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE); | |
201 | ||
d22ddcbc RW |
202 | offline = false; |
203 | break; | |
204 | } | |
205 | ||
206 | mutex_unlock(&adev->physical_node_lock); | |
207 | return offline; | |
208 | } | |
209 | ||
7f28ddec RW |
210 | static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data, |
211 | void **ret_p) | |
683058e3 RW |
212 | { |
213 | struct acpi_device *device = NULL; | |
214 | struct acpi_device_physical_node *pn; | |
303bfdb1 | 215 | bool second_pass = (bool)data; |
683058e3 RW |
216 | acpi_status status = AE_OK; |
217 | ||
218 | if (acpi_bus_get_device(handle, &device)) | |
219 | return AE_OK; | |
220 | ||
7f28ddec RW |
221 | if (device->handler && !device->handler->hotplug.enabled) { |
222 | *ret_p = &device->dev; | |
223 | return AE_SUPPORT; | |
224 | } | |
225 | ||
683058e3 RW |
226 | mutex_lock(&device->physical_node_lock); |
227 | ||
228 | list_for_each_entry(pn, &device->physical_node_list, node) { | |
229 | int ret; | |
230 | ||
303bfdb1 RW |
231 | if (second_pass) { |
232 | /* Skip devices offlined by the first pass. */ | |
233 | if (pn->put_online) | |
234 | continue; | |
235 | } else { | |
236 | pn->put_online = false; | |
237 | } | |
683058e3 RW |
238 | ret = device_offline(pn->dev); |
239 | if (acpi_force_hot_remove) | |
240 | continue; | |
241 | ||
303bfdb1 RW |
242 | if (ret >= 0) { |
243 | pn->put_online = !ret; | |
244 | } else { | |
245 | *ret_p = pn->dev; | |
246 | if (second_pass) { | |
247 | status = AE_ERROR; | |
248 | break; | |
249 | } | |
683058e3 | 250 | } |
683058e3 RW |
251 | } |
252 | ||
253 | mutex_unlock(&device->physical_node_lock); | |
254 | ||
255 | return status; | |
256 | } | |
257 | ||
7f28ddec RW |
258 | static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data, |
259 | void **ret_p) | |
683058e3 RW |
260 | { |
261 | struct acpi_device *device = NULL; | |
262 | struct acpi_device_physical_node *pn; | |
263 | ||
264 | if (acpi_bus_get_device(handle, &device)) | |
265 | return AE_OK; | |
266 | ||
267 | mutex_lock(&device->physical_node_lock); | |
268 | ||
269 | list_for_each_entry(pn, &device->physical_node_list, node) | |
270 | if (pn->put_online) { | |
271 | device_online(pn->dev); | |
272 | pn->put_online = false; | |
273 | } | |
274 | ||
275 | mutex_unlock(&device->physical_node_lock); | |
276 | ||
277 | return AE_OK; | |
278 | } | |
279 | ||
d22ddcbc | 280 | static int acpi_scan_try_to_offline(struct acpi_device *device) |
1da177e4 | 281 | { |
5993c467 | 282 | acpi_handle handle = device->handle; |
d22ddcbc | 283 | struct device *errdev = NULL; |
a33ec399 | 284 | acpi_status status; |
f058cdf4 | 285 | |
303bfdb1 RW |
286 | /* |
287 | * Carry out two passes here and ignore errors in the first pass, | |
288 | * because if the devices in question are memory blocks and | |
289 | * CONFIG_MEMCG is set, one of the blocks may hold data structures | |
290 | * that the other blocks depend on, but it is not known in advance which | |
291 | * block holds them. | |
292 | * | |
293 | * If the first pass is successful, the second one isn't needed, though. | |
294 | */ | |
7f28ddec RW |
295 | status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
296 | NULL, acpi_bus_offline, (void *)false, | |
297 | (void **)&errdev); | |
298 | if (status == AE_SUPPORT) { | |
299 | dev_warn(errdev, "Offline disabled.\n"); | |
300 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, | |
301 | acpi_bus_online, NULL, NULL, NULL); | |
7f28ddec RW |
302 | return -EPERM; |
303 | } | |
304 | acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev); | |
303bfdb1 RW |
305 | if (errdev) { |
306 | errdev = NULL; | |
683058e3 | 307 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
7f28ddec RW |
308 | NULL, acpi_bus_offline, (void *)true, |
309 | (void **)&errdev); | |
303bfdb1 | 310 | if (!errdev || acpi_force_hot_remove) |
7f28ddec RW |
311 | acpi_bus_offline(handle, 0, (void *)true, |
312 | (void **)&errdev); | |
303bfdb1 RW |
313 | |
314 | if (errdev && !acpi_force_hot_remove) { | |
315 | dev_warn(errdev, "Offline failed.\n"); | |
7f28ddec | 316 | acpi_bus_online(handle, 0, NULL, NULL); |
303bfdb1 | 317 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, |
7f28ddec RW |
318 | ACPI_UINT32_MAX, acpi_bus_online, |
319 | NULL, NULL, NULL); | |
303bfdb1 RW |
320 | return -EBUSY; |
321 | } | |
683058e3 | 322 | } |
d22ddcbc RW |
323 | return 0; |
324 | } | |
325 | ||
326 | static int acpi_scan_hot_remove(struct acpi_device *device) | |
327 | { | |
328 | acpi_handle handle = device->handle; | |
329 | unsigned long long sta; | |
330 | acpi_status status; | |
331 | ||
332 | if (device->handler->hotplug.demand_offline && !acpi_force_hot_remove) { | |
caa73ea1 | 333 | if (!acpi_scan_is_offline(device, true)) |
d22ddcbc RW |
334 | return -EBUSY; |
335 | } else { | |
336 | int error = acpi_scan_try_to_offline(device); | |
337 | if (error) | |
338 | return error; | |
339 | } | |
683058e3 | 340 | |
26d46867 | 341 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
0794469d | 342 | "Hot-removing device %s...\n", dev_name(&device->dev))); |
26d46867 | 343 | |
bfee26db | 344 | acpi_bus_trim(device); |
683058e3 | 345 | |
7d2421f8 | 346 | acpi_evaluate_lck(handle, 0); |
1da177e4 LT |
347 | /* |
348 | * TBD: _EJD support. | |
349 | */ | |
7d2421f8 JL |
350 | status = acpi_evaluate_ej0(handle); |
351 | if (status == AE_NOT_FOUND) | |
352 | return -ENODEV; | |
353 | else if (ACPI_FAILURE(status)) | |
354 | return -EIO; | |
1da177e4 | 355 | |
882fd12e TK |
356 | /* |
357 | * Verify if eject was indeed successful. If not, log an error | |
358 | * message. No need to call _OST since _EJ0 call was made OK. | |
359 | */ | |
360 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); | |
361 | if (ACPI_FAILURE(status)) { | |
362 | acpi_handle_warn(handle, | |
363 | "Status check after eject failed (0x%x)\n", status); | |
364 | } else if (sta & ACPI_STA_DEVICE_ENABLED) { | |
365 | acpi_handle_warn(handle, | |
366 | "Eject incomplete - status 0x%llx\n", sta); | |
367 | } | |
368 | ||
a33ec399 RW |
369 | return 0; |
370 | } | |
1da177e4 | 371 | |
443fc820 RW |
372 | static int acpi_scan_device_not_present(struct acpi_device *adev) |
373 | { | |
374 | if (!acpi_device_enumerated(adev)) { | |
375 | dev_warn(&adev->dev, "Still not present\n"); | |
376 | return -EALREADY; | |
377 | } | |
378 | acpi_bus_trim(adev); | |
379 | return 0; | |
380 | } | |
381 | ||
c27b2c33 | 382 | static int acpi_scan_device_check(struct acpi_device *adev) |
a33ec399 | 383 | { |
f943db40 | 384 | int error; |
a33ec399 | 385 | |
443fc820 RW |
386 | acpi_bus_get_status(adev); |
387 | if (adev->status.present || adev->status.functional) { | |
388 | /* | |
389 | * This function is only called for device objects for which | |
390 | * matching scan handlers exist. The only situation in which | |
391 | * the scan handler is not attached to this device object yet | |
392 | * is when the device has just appeared (either it wasn't | |
393 | * present at all before or it was removed and then added | |
394 | * again). | |
395 | */ | |
396 | if (adev->handler) { | |
397 | dev_warn(&adev->dev, "Already enumerated\n"); | |
398 | return -EALREADY; | |
399 | } | |
400 | error = acpi_bus_scan(adev->handle); | |
401 | if (error) { | |
402 | dev_warn(&adev->dev, "Namespace scan failure\n"); | |
403 | return error; | |
404 | } | |
405 | if (!adev->handler) { | |
406 | dev_warn(&adev->dev, "Enumeration failure\n"); | |
407 | error = -ENODEV; | |
408 | } | |
409 | } else { | |
410 | error = acpi_scan_device_not_present(adev); | |
411 | } | |
412 | return error; | |
413 | } | |
414 | ||
415 | static int acpi_scan_bus_check(struct acpi_device *adev) | |
416 | { | |
417 | struct acpi_scan_handler *handler = adev->handler; | |
418 | struct acpi_device *child; | |
419 | int error; | |
420 | ||
421 | acpi_bus_get_status(adev); | |
422 | if (!(adev->status.present || adev->status.functional)) { | |
423 | acpi_scan_device_not_present(adev); | |
424 | return 0; | |
425 | } | |
426 | if (handler && handler->hotplug.scan_dependent) | |
427 | return handler->hotplug.scan_dependent(adev); | |
428 | ||
c27b2c33 RW |
429 | error = acpi_bus_scan(adev->handle); |
430 | if (error) { | |
431 | dev_warn(&adev->dev, "Namespace scan failure\n"); | |
432 | return error; | |
433 | } | |
443fc820 RW |
434 | list_for_each_entry(child, &adev->children, node) { |
435 | error = acpi_scan_bus_check(child); | |
436 | if (error) | |
437 | return error; | |
c27b2c33 RW |
438 | } |
439 | return 0; | |
a33ec399 RW |
440 | } |
441 | ||
3338db00 | 442 | static void acpi_device_hotplug(void *data, u32 src) |
a33ec399 | 443 | { |
a33ec399 | 444 | u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; |
c27b2c33 | 445 | struct acpi_device *adev = data; |
a33ec399 RW |
446 | int error; |
447 | ||
683058e3 | 448 | lock_device_hotplug(); |
e0ae8fee | 449 | mutex_lock(&acpi_scan_lock); |
a33ec399 | 450 | |
c27b2c33 RW |
451 | /* |
452 | * The device object's ACPI handle cannot become invalid as long as we | |
453 | * are holding acpi_scan_lock, but it may have become invalid before | |
454 | * that lock was acquired. | |
455 | */ | |
456 | if (adev->handle == INVALID_ACPI_HANDLE) | |
a33ec399 | 457 | goto out; |
c27b2c33 RW |
458 | |
459 | switch (src) { | |
460 | case ACPI_NOTIFY_BUS_CHECK: | |
443fc820 | 461 | error = acpi_scan_bus_check(adev); |
c27b2c33 RW |
462 | break; |
463 | case ACPI_NOTIFY_DEVICE_CHECK: | |
443fc820 | 464 | error = acpi_scan_device_check(adev); |
c27b2c33 RW |
465 | break; |
466 | case ACPI_NOTIFY_EJECT_REQUEST: | |
467 | case ACPI_OST_EC_OSPM_EJECT: | |
468 | error = acpi_scan_hot_remove(adev); | |
469 | break; | |
470 | default: | |
471 | error = -EINVAL; | |
472 | break; | |
a33ec399 | 473 | } |
c27b2c33 RW |
474 | if (!error) |
475 | ost_code = ACPI_OST_SC_SUCCESS; | |
a33ec399 RW |
476 | |
477 | out: | |
c27b2c33 | 478 | acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL); |
78ea4639 | 479 | acpi_bus_put_acpi_device(adev); |
a33ec399 | 480 | mutex_unlock(&acpi_scan_lock); |
e0ae8fee | 481 | unlock_device_hotplug(); |
a33ec399 RW |
482 | } |
483 | ||
2cbb14fb | 484 | static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data) |
a33ec399 | 485 | { |
1ceaba05 | 486 | u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; |
2cbb14fb | 487 | struct acpi_scan_handler *handler = data; |
a3b1b1ef | 488 | struct acpi_device *adev; |
a33ec399 RW |
489 | acpi_status status; |
490 | ||
491 | switch (type) { | |
492 | case ACPI_NOTIFY_BUS_CHECK: | |
493 | acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n"); | |
a33ec399 RW |
494 | break; |
495 | case ACPI_NOTIFY_DEVICE_CHECK: | |
496 | acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n"); | |
a33ec399 RW |
497 | break; |
498 | case ACPI_NOTIFY_EJECT_REQUEST: | |
499 | acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n"); | |
1ceaba05 RW |
500 | if (!handler->hotplug.enabled) { |
501 | acpi_handle_err(handle, "Eject disabled\n"); | |
502 | ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED; | |
503 | goto err_out; | |
504 | } | |
c27b2c33 RW |
505 | acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, |
506 | ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); | |
507 | break; | |
a33ec399 RW |
508 | default: |
509 | /* non-hotplug event; possibly handled by other handler */ | |
510 | return; | |
511 | } | |
78ea4639 RW |
512 | adev = acpi_bus_get_acpi_device(handle); |
513 | if (!adev) | |
514 | goto err_out; | |
515 | ||
c27b2c33 | 516 | status = acpi_hotplug_execute(acpi_device_hotplug, adev, type); |
a3b1b1ef RW |
517 | if (ACPI_SUCCESS(status)) |
518 | return; | |
a33ec399 | 519 | |
78ea4639 | 520 | acpi_bus_put_acpi_device(adev); |
c27b2c33 | 521 | |
a3b1b1ef | 522 | err_out: |
1ceaba05 | 523 | acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL); |
1da177e4 LT |
524 | } |
525 | ||
836aedb1 RW |
526 | static ssize_t real_power_state_show(struct device *dev, |
527 | struct device_attribute *attr, char *buf) | |
528 | { | |
529 | struct acpi_device *adev = to_acpi_device(dev); | |
530 | int state; | |
531 | int ret; | |
532 | ||
533 | ret = acpi_device_get_power(adev, &state); | |
534 | if (ret) | |
535 | return ret; | |
536 | ||
537 | return sprintf(buf, "%s\n", acpi_power_state_string(state)); | |
538 | } | |
539 | ||
540 | static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL); | |
541 | ||
542 | static ssize_t power_state_show(struct device *dev, | |
543 | struct device_attribute *attr, char *buf) | |
544 | { | |
545 | struct acpi_device *adev = to_acpi_device(dev); | |
546 | ||
547 | return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state)); | |
548 | } | |
549 | ||
550 | static DEVICE_ATTR(power_state, 0444, power_state_show, NULL); | |
551 | ||
1da177e4 | 552 | static ssize_t |
f883d9db PM |
553 | acpi_eject_store(struct device *d, struct device_attribute *attr, |
554 | const char *buf, size_t count) | |
1da177e4 | 555 | { |
f883d9db | 556 | struct acpi_device *acpi_device = to_acpi_device(d); |
a33ec399 RW |
557 | acpi_object_type not_used; |
558 | acpi_status status; | |
1da177e4 | 559 | |
a33ec399 | 560 | if (!count || buf[0] != '1') |
1da177e4 | 561 | return -EINVAL; |
1da177e4 | 562 | |
a33ec399 RW |
563 | if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled) |
564 | && !acpi_device->driver) | |
565 | return -ENODEV; | |
566 | ||
567 | status = acpi_get_type(acpi_device->handle, ¬_used); | |
568 | if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable) | |
569 | return -ENODEV; | |
570 | ||
f943db40 | 571 | acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT, |
a33ec399 | 572 | ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); |
a33ec399 | 573 | get_device(&acpi_device->dev); |
c27b2c33 | 574 | status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device, |
7b98118a | 575 | ACPI_OST_EC_OSPM_EJECT); |
f943db40 RW |
576 | if (ACPI_SUCCESS(status)) |
577 | return count; | |
a33ec399 | 578 | |
f943db40 | 579 | put_device(&acpi_device->dev); |
f943db40 | 580 | acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT, |
a33ec399 | 581 | ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL); |
5add99cf | 582 | return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN; |
1da177e4 LT |
583 | } |
584 | ||
f883d9db | 585 | static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); |
1da177e4 | 586 | |
e49bd2dd ZR |
587 | static ssize_t |
588 | acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) { | |
589 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
1da177e4 | 590 | |
ea8d82fd | 591 | return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev)); |
1da177e4 | 592 | } |
e49bd2dd | 593 | static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL); |
1da177e4 | 594 | |
923d4a45 LZ |
595 | static ssize_t acpi_device_uid_show(struct device *dev, |
596 | struct device_attribute *attr, char *buf) | |
597 | { | |
598 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
599 | ||
600 | return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id); | |
601 | } | |
602 | static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL); | |
603 | ||
604 | static ssize_t acpi_device_adr_show(struct device *dev, | |
605 | struct device_attribute *attr, char *buf) | |
606 | { | |
607 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
608 | ||
609 | return sprintf(buf, "0x%08x\n", | |
610 | (unsigned int)(acpi_dev->pnp.bus_address)); | |
611 | } | |
612 | static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL); | |
613 | ||
e49bd2dd ZR |
614 | static ssize_t |
615 | acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) { | |
616 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
617 | struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; | |
618 | int result; | |
1da177e4 | 619 | |
e49bd2dd | 620 | result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path); |
0c526d96 | 621 | if (result) |
e49bd2dd | 622 | goto end; |
1da177e4 | 623 | |
e49bd2dd ZR |
624 | result = sprintf(buf, "%s\n", (char*)path.pointer); |
625 | kfree(path.pointer); | |
0c526d96 | 626 | end: |
e49bd2dd | 627 | return result; |
1da177e4 | 628 | } |
e49bd2dd | 629 | static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL); |
1da177e4 | 630 | |
d1efe3c3 LO |
631 | /* sysfs file that shows description text from the ACPI _STR method */ |
632 | static ssize_t description_show(struct device *dev, | |
633 | struct device_attribute *attr, | |
634 | char *buf) { | |
635 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
636 | int result; | |
637 | ||
638 | if (acpi_dev->pnp.str_obj == NULL) | |
639 | return 0; | |
640 | ||
641 | /* | |
642 | * The _STR object contains a Unicode identifier for a device. | |
643 | * We need to convert to utf-8 so it can be displayed. | |
644 | */ | |
645 | result = utf16s_to_utf8s( | |
646 | (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer, | |
647 | acpi_dev->pnp.str_obj->buffer.length, | |
648 | UTF16_LITTLE_ENDIAN, buf, | |
649 | PAGE_SIZE); | |
650 | ||
651 | buf[result++] = '\n'; | |
652 | ||
653 | return result; | |
654 | } | |
655 | static DEVICE_ATTR(description, 0444, description_show, NULL); | |
656 | ||
bb74ac23 YI |
657 | static ssize_t |
658 | acpi_device_sun_show(struct device *dev, struct device_attribute *attr, | |
659 | char *buf) { | |
660 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
661 | ||
662 | return sprintf(buf, "%lu\n", acpi_dev->pnp.sun); | |
663 | } | |
664 | static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL); | |
665 | ||
c713cd7f SP |
666 | static ssize_t status_show(struct device *dev, struct device_attribute *attr, |
667 | char *buf) { | |
668 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
669 | acpi_status status; | |
670 | unsigned long long sta; | |
671 | ||
672 | status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta); | |
673 | if (ACPI_FAILURE(status)) | |
674 | return -ENODEV; | |
675 | ||
676 | return sprintf(buf, "%llu\n", sta); | |
677 | } | |
678 | static DEVICE_ATTR_RO(status); | |
679 | ||
e49bd2dd | 680 | static int acpi_device_setup_files(struct acpi_device *dev) |
1da177e4 | 681 | { |
d1efe3c3 | 682 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
f883d9db | 683 | acpi_status status; |
bb74ac23 | 684 | unsigned long long sun; |
e49bd2dd | 685 | int result = 0; |
1da177e4 LT |
686 | |
687 | /* | |
e49bd2dd | 688 | * Devices gotten from FADT don't have a "path" attribute |
1da177e4 | 689 | */ |
0c526d96 | 690 | if (dev->handle) { |
e49bd2dd | 691 | result = device_create_file(&dev->dev, &dev_attr_path); |
0c526d96 | 692 | if (result) |
e49bd2dd | 693 | goto end; |
1da177e4 LT |
694 | } |
695 | ||
2b2ae7c7 TR |
696 | if (!list_empty(&dev->pnp.ids)) { |
697 | result = device_create_file(&dev->dev, &dev_attr_hid); | |
698 | if (result) | |
699 | goto end; | |
1da177e4 | 700 | |
2b2ae7c7 TR |
701 | result = device_create_file(&dev->dev, &dev_attr_modalias); |
702 | if (result) | |
703 | goto end; | |
704 | } | |
29b71a1c | 705 | |
d1efe3c3 LO |
706 | /* |
707 | * If device has _STR, 'description' file is created | |
708 | */ | |
952c63e9 | 709 | if (acpi_has_method(dev->handle, "_STR")) { |
d1efe3c3 LO |
710 | status = acpi_evaluate_object(dev->handle, "_STR", |
711 | NULL, &buffer); | |
712 | if (ACPI_FAILURE(status)) | |
713 | buffer.pointer = NULL; | |
714 | dev->pnp.str_obj = buffer.pointer; | |
715 | result = device_create_file(&dev->dev, &dev_attr_description); | |
716 | if (result) | |
717 | goto end; | |
718 | } | |
719 | ||
d4e1a692 | 720 | if (dev->pnp.type.bus_address) |
923d4a45 LZ |
721 | result = device_create_file(&dev->dev, &dev_attr_adr); |
722 | if (dev->pnp.unique_id) | |
723 | result = device_create_file(&dev->dev, &dev_attr_uid); | |
724 | ||
bb74ac23 YI |
725 | status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun); |
726 | if (ACPI_SUCCESS(status)) { | |
727 | dev->pnp.sun = (unsigned long)sun; | |
728 | result = device_create_file(&dev->dev, &dev_attr_sun); | |
729 | if (result) | |
730 | goto end; | |
731 | } else { | |
732 | dev->pnp.sun = (unsigned long)-1; | |
733 | } | |
734 | ||
c713cd7f SP |
735 | if (acpi_has_method(dev->handle, "_STA")) { |
736 | result = device_create_file(&dev->dev, &dev_attr_status); | |
737 | if (result) | |
738 | goto end; | |
739 | } | |
740 | ||
e49bd2dd ZR |
741 | /* |
742 | * If device has _EJ0, 'eject' file is created that is used to trigger | |
743 | * hot-removal function from userland. | |
744 | */ | |
952c63e9 | 745 | if (acpi_has_method(dev->handle, "_EJ0")) { |
e49bd2dd | 746 | result = device_create_file(&dev->dev, &dev_attr_eject); |
836aedb1 RW |
747 | if (result) |
748 | return result; | |
749 | } | |
750 | ||
751 | if (dev->flags.power_manageable) { | |
752 | result = device_create_file(&dev->dev, &dev_attr_power_state); | |
753 | if (result) | |
754 | return result; | |
755 | ||
756 | if (dev->power.flags.power_resources) | |
757 | result = device_create_file(&dev->dev, | |
758 | &dev_attr_real_power_state); | |
759 | } | |
760 | ||
0c526d96 | 761 | end: |
e49bd2dd | 762 | return result; |
1da177e4 LT |
763 | } |
764 | ||
f883d9db | 765 | static void acpi_device_remove_files(struct acpi_device *dev) |
1da177e4 | 766 | { |
836aedb1 RW |
767 | if (dev->flags.power_manageable) { |
768 | device_remove_file(&dev->dev, &dev_attr_power_state); | |
769 | if (dev->power.flags.power_resources) | |
770 | device_remove_file(&dev->dev, | |
771 | &dev_attr_real_power_state); | |
772 | } | |
773 | ||
f883d9db | 774 | /* |
d1efe3c3 LO |
775 | * If device has _STR, remove 'description' file |
776 | */ | |
952c63e9 | 777 | if (acpi_has_method(dev->handle, "_STR")) { |
d1efe3c3 LO |
778 | kfree(dev->pnp.str_obj); |
779 | device_remove_file(&dev->dev, &dev_attr_description); | |
780 | } | |
781 | /* | |
782 | * If device has _EJ0, remove 'eject' file. | |
f883d9db | 783 | */ |
952c63e9 | 784 | if (acpi_has_method(dev->handle, "_EJ0")) |
f883d9db | 785 | device_remove_file(&dev->dev, &dev_attr_eject); |
1da177e4 | 786 | |
952c63e9 | 787 | if (acpi_has_method(dev->handle, "_SUN")) |
bb74ac23 YI |
788 | device_remove_file(&dev->dev, &dev_attr_sun); |
789 | ||
923d4a45 LZ |
790 | if (dev->pnp.unique_id) |
791 | device_remove_file(&dev->dev, &dev_attr_uid); | |
d4e1a692 | 792 | if (dev->pnp.type.bus_address) |
923d4a45 | 793 | device_remove_file(&dev->dev, &dev_attr_adr); |
1131b938 BH |
794 | device_remove_file(&dev->dev, &dev_attr_modalias); |
795 | device_remove_file(&dev->dev, &dev_attr_hid); | |
c713cd7f SP |
796 | if (acpi_has_method(dev->handle, "_STA")) |
797 | device_remove_file(&dev->dev, &dev_attr_status); | |
0c526d96 | 798 | if (dev->handle) |
e49bd2dd | 799 | device_remove_file(&dev->dev, &dev_attr_path); |
1da177e4 | 800 | } |
1da177e4 | 801 | /* -------------------------------------------------------------------------- |
9e89dde2 | 802 | ACPI Bus operations |
1da177e4 | 803 | -------------------------------------------------------------------------- */ |
29b71a1c | 804 | |
cf761af9 MW |
805 | static const struct acpi_device_id *__acpi_match_device( |
806 | struct acpi_device *device, const struct acpi_device_id *ids) | |
29b71a1c TR |
807 | { |
808 | const struct acpi_device_id *id; | |
7f47fa6c | 809 | struct acpi_hardware_id *hwid; |
29b71a1c | 810 | |
39a0ad87 ZY |
811 | /* |
812 | * If the device is not present, it is unnecessary to load device | |
813 | * driver for it. | |
814 | */ | |
815 | if (!device->status.present) | |
cf761af9 | 816 | return NULL; |
39a0ad87 | 817 | |
7f47fa6c BH |
818 | for (id = ids; id->id[0]; id++) |
819 | list_for_each_entry(hwid, &device->pnp.ids, list) | |
820 | if (!strcmp((char *) id->id, hwid->id)) | |
cf761af9 MW |
821 | return id; |
822 | ||
823 | return NULL; | |
824 | } | |
825 | ||
826 | /** | |
827 | * acpi_match_device - Match a struct device against a given list of ACPI IDs | |
828 | * @ids: Array of struct acpi_device_id object to match against. | |
829 | * @dev: The device structure to match. | |
830 | * | |
831 | * Check if @dev has a valid ACPI handle and if there is a struct acpi_device | |
832 | * object for that handle and use that object to match against a given list of | |
833 | * device IDs. | |
834 | * | |
835 | * Return a pointer to the first matching ID on success or %NULL on failure. | |
836 | */ | |
837 | const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, | |
838 | const struct device *dev) | |
839 | { | |
840 | struct acpi_device *adev; | |
0613e1f7 | 841 | acpi_handle handle = ACPI_HANDLE(dev); |
cf761af9 | 842 | |
0613e1f7 | 843 | if (!ids || !handle || acpi_bus_get_device(handle, &adev)) |
cf761af9 MW |
844 | return NULL; |
845 | ||
846 | return __acpi_match_device(adev, ids); | |
847 | } | |
848 | EXPORT_SYMBOL_GPL(acpi_match_device); | |
29b71a1c | 849 | |
cf761af9 MW |
850 | int acpi_match_device_ids(struct acpi_device *device, |
851 | const struct acpi_device_id *ids) | |
852 | { | |
853 | return __acpi_match_device(device, ids) ? 0 : -ENOENT; | |
29b71a1c TR |
854 | } |
855 | EXPORT_SYMBOL(acpi_match_device_ids); | |
856 | ||
0b224527 RW |
857 | static void acpi_free_power_resources_lists(struct acpi_device *device) |
858 | { | |
859 | int i; | |
860 | ||
993cbe59 RW |
861 | if (device->wakeup.flags.valid) |
862 | acpi_power_resources_list_free(&device->wakeup.resources); | |
863 | ||
0b224527 RW |
864 | if (!device->flags.power_manageable) |
865 | return; | |
866 | ||
867 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { | |
868 | struct acpi_device_power_state *ps = &device->power.states[i]; | |
869 | acpi_power_resources_list_free(&ps->resources); | |
870 | } | |
7f47fa6c BH |
871 | } |
872 | ||
1890a97a | 873 | static void acpi_device_release(struct device *dev) |
1da177e4 | 874 | { |
1890a97a | 875 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
1da177e4 | 876 | |
c0af4175 | 877 | acpi_free_pnp_ids(&acpi_dev->pnp); |
0b224527 | 878 | acpi_free_power_resources_lists(acpi_dev); |
1890a97a | 879 | kfree(acpi_dev); |
1da177e4 LT |
880 | } |
881 | ||
5d9464a4 | 882 | static int acpi_bus_match(struct device *dev, struct device_driver *drv) |
9e89dde2 | 883 | { |
5d9464a4 PM |
884 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
885 | struct acpi_driver *acpi_drv = to_acpi_driver(drv); | |
1da177e4 | 886 | |
209d3b17 | 887 | return acpi_dev->flags.match_driver |
805d410f | 888 | && !acpi_match_device_ids(acpi_dev, acpi_drv->ids); |
5d9464a4 | 889 | } |
1da177e4 | 890 | |
7eff2e7a | 891 | static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
1da177e4 | 892 | { |
5d9464a4 | 893 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
7eff2e7a | 894 | int len; |
5d9464a4 | 895 | |
2b2ae7c7 TR |
896 | if (list_empty(&acpi_dev->pnp.ids)) |
897 | return 0; | |
898 | ||
7eff2e7a KS |
899 | if (add_uevent_var(env, "MODALIAS=")) |
900 | return -ENOMEM; | |
901 | len = create_modalias(acpi_dev, &env->buf[env->buflen - 1], | |
902 | sizeof(env->buf) - env->buflen); | |
3d8e0090 ZR |
903 | if (len <= 0) |
904 | return len; | |
7eff2e7a | 905 | env->buflen += len; |
9e89dde2 | 906 | return 0; |
1da177e4 LT |
907 | } |
908 | ||
46ec8598 BH |
909 | static void acpi_device_notify(acpi_handle handle, u32 event, void *data) |
910 | { | |
911 | struct acpi_device *device = data; | |
912 | ||
913 | device->driver->ops.notify(device, event); | |
914 | } | |
915 | ||
916 | static acpi_status acpi_device_notify_fixed(void *data) | |
917 | { | |
918 | struct acpi_device *device = data; | |
919 | ||
53de5356 BH |
920 | /* Fixed hardware devices have no handles */ |
921 | acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); | |
46ec8598 BH |
922 | return AE_OK; |
923 | } | |
924 | ||
925 | static int acpi_device_install_notify_handler(struct acpi_device *device) | |
926 | { | |
927 | acpi_status status; | |
46ec8598 | 928 | |
ccba2a36 | 929 | if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) |
46ec8598 BH |
930 | status = |
931 | acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, | |
932 | acpi_device_notify_fixed, | |
933 | device); | |
ccba2a36 | 934 | else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) |
46ec8598 BH |
935 | status = |
936 | acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, | |
937 | acpi_device_notify_fixed, | |
938 | device); | |
939 | else | |
940 | status = acpi_install_notify_handler(device->handle, | |
941 | ACPI_DEVICE_NOTIFY, | |
942 | acpi_device_notify, | |
943 | device); | |
944 | ||
945 | if (ACPI_FAILURE(status)) | |
946 | return -EINVAL; | |
947 | return 0; | |
948 | } | |
949 | ||
950 | static void acpi_device_remove_notify_handler(struct acpi_device *device) | |
951 | { | |
ccba2a36 | 952 | if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) |
46ec8598 BH |
953 | acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, |
954 | acpi_device_notify_fixed); | |
ccba2a36 | 955 | else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) |
46ec8598 BH |
956 | acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, |
957 | acpi_device_notify_fixed); | |
958 | else | |
959 | acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY, | |
960 | acpi_device_notify); | |
961 | } | |
962 | ||
d9e455f5 | 963 | static int acpi_device_probe(struct device *dev) |
1da177e4 | 964 | { |
5d9464a4 PM |
965 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
966 | struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); | |
967 | int ret; | |
968 | ||
24071f47 RW |
969 | if (acpi_dev->handler) |
970 | return -EINVAL; | |
971 | ||
d9e455f5 RW |
972 | if (!acpi_drv->ops.add) |
973 | return -ENOSYS; | |
46ec8598 | 974 | |
d9e455f5 RW |
975 | ret = acpi_drv->ops.add(acpi_dev); |
976 | if (ret) | |
977 | return ret; | |
46ec8598 | 978 | |
d9e455f5 RW |
979 | acpi_dev->driver = acpi_drv; |
980 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
981 | "Driver [%s] successfully bound to device [%s]\n", | |
982 | acpi_drv->name, acpi_dev->pnp.bus_id)); | |
983 | ||
984 | if (acpi_drv->ops.notify) { | |
985 | ret = acpi_device_install_notify_handler(acpi_dev); | |
986 | if (ret) { | |
987 | if (acpi_drv->ops.remove) | |
988 | acpi_drv->ops.remove(acpi_dev); | |
989 | ||
990 | acpi_dev->driver = NULL; | |
991 | acpi_dev->driver_data = NULL; | |
992 | return ret; | |
993 | } | |
5d9464a4 | 994 | } |
d9e455f5 RW |
995 | |
996 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n", | |
997 | acpi_drv->name, acpi_dev->pnp.bus_id)); | |
998 | get_device(dev); | |
999 | return 0; | |
5d9464a4 | 1000 | } |
1da177e4 | 1001 | |
5d9464a4 PM |
1002 | static int acpi_device_remove(struct device * dev) |
1003 | { | |
1004 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
1005 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
1006 | ||
1007 | if (acpi_drv) { | |
46ec8598 BH |
1008 | if (acpi_drv->ops.notify) |
1009 | acpi_device_remove_notify_handler(acpi_dev); | |
5d9464a4 | 1010 | if (acpi_drv->ops.remove) |
51fac838 | 1011 | acpi_drv->ops.remove(acpi_dev); |
1da177e4 | 1012 | } |
5d9464a4 | 1013 | acpi_dev->driver = NULL; |
db89b4f0 | 1014 | acpi_dev->driver_data = NULL; |
1da177e4 | 1015 | |
5d9464a4 | 1016 | put_device(dev); |
9e89dde2 ZR |
1017 | return 0; |
1018 | } | |
1da177e4 | 1019 | |
55955aad | 1020 | struct bus_type acpi_bus_type = { |
9e89dde2 | 1021 | .name = "acpi", |
5d9464a4 PM |
1022 | .match = acpi_bus_match, |
1023 | .probe = acpi_device_probe, | |
1024 | .remove = acpi_device_remove, | |
1025 | .uevent = acpi_device_uevent, | |
9e89dde2 ZR |
1026 | }; |
1027 | ||
d783156e RW |
1028 | static void acpi_device_del(struct acpi_device *device) |
1029 | { | |
1030 | mutex_lock(&acpi_device_lock); | |
1031 | if (device->parent) | |
1032 | list_del(&device->node); | |
1033 | ||
1034 | list_del(&device->wakeup_list); | |
1035 | mutex_unlock(&acpi_device_lock); | |
1036 | ||
1037 | acpi_power_add_remove_device(device, false); | |
1038 | acpi_device_remove_files(device); | |
1039 | if (device->remove) | |
1040 | device->remove(device); | |
1041 | ||
1042 | device_del(&device->dev); | |
1043 | } | |
1044 | ||
1045 | static LIST_HEAD(acpi_device_del_list); | |
1046 | static DEFINE_MUTEX(acpi_device_del_lock); | |
1047 | ||
1048 | static void acpi_device_del_work_fn(struct work_struct *work_not_used) | |
1049 | { | |
1050 | for (;;) { | |
1051 | struct acpi_device *adev; | |
1052 | ||
1053 | mutex_lock(&acpi_device_del_lock); | |
1054 | ||
1055 | if (list_empty(&acpi_device_del_list)) { | |
1056 | mutex_unlock(&acpi_device_del_lock); | |
1057 | break; | |
1058 | } | |
1059 | adev = list_first_entry(&acpi_device_del_list, | |
1060 | struct acpi_device, del_list); | |
1061 | list_del(&adev->del_list); | |
1062 | ||
1063 | mutex_unlock(&acpi_device_del_lock); | |
1064 | ||
1065 | acpi_device_del(adev); | |
1066 | /* | |
1067 | * Drop references to all power resources that might have been | |
1068 | * used by the device. | |
1069 | */ | |
1070 | acpi_power_transition(adev, ACPI_STATE_D3_COLD); | |
1071 | put_device(&adev->dev); | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | /** | |
1076 | * acpi_scan_drop_device - Drop an ACPI device object. | |
1077 | * @handle: Handle of an ACPI namespace node, not used. | |
1078 | * @context: Address of the ACPI device object to drop. | |
1079 | * | |
1080 | * This is invoked by acpi_ns_delete_node() during the removal of the ACPI | |
1081 | * namespace node the device object pointed to by @context is attached to. | |
1082 | * | |
1083 | * The unregistration is carried out asynchronously to avoid running | |
1084 | * acpi_device_del() under the ACPICA's namespace mutex and the list is used to | |
1085 | * ensure the correct ordering (the device objects must be unregistered in the | |
1086 | * same order in which the corresponding namespace nodes are deleted). | |
1087 | */ | |
1088 | static void acpi_scan_drop_device(acpi_handle handle, void *context) | |
caf5c03f | 1089 | { |
d783156e RW |
1090 | static DECLARE_WORK(work, acpi_device_del_work_fn); |
1091 | struct acpi_device *adev = context; | |
1092 | ||
1093 | mutex_lock(&acpi_device_del_lock); | |
1094 | ||
1095 | /* | |
1096 | * Use the ACPI hotplug workqueue which is ordered, so this work item | |
1097 | * won't run after any hotplug work items submitted subsequently. That | |
1098 | * prevents attempts to register device objects identical to those being | |
1099 | * deleted from happening concurrently (such attempts result from | |
1100 | * hotplug events handled via the ACPI hotplug workqueue). It also will | |
1101 | * run after all of the work items submitted previosuly, which helps | |
1102 | * those work items to ensure that they are not accessing stale device | |
1103 | * objects. | |
1104 | */ | |
1105 | if (list_empty(&acpi_device_del_list)) | |
1106 | acpi_queue_hotplug_work(&work); | |
1107 | ||
1108 | list_add_tail(&adev->del_list, &acpi_device_del_list); | |
1109 | /* Make acpi_ns_validate_handle() return NULL for this handle. */ | |
1110 | adev->handle = INVALID_ACPI_HANDLE; | |
1111 | ||
1112 | mutex_unlock(&acpi_device_del_lock); | |
caf5c03f RW |
1113 | } |
1114 | ||
78ea4639 RW |
1115 | static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device, |
1116 | void (*callback)(void *)) | |
caf5c03f RW |
1117 | { |
1118 | acpi_status status; | |
1119 | ||
1120 | if (!device) | |
1121 | return -EINVAL; | |
1122 | ||
78ea4639 RW |
1123 | status = acpi_get_data_full(handle, acpi_scan_drop_device, |
1124 | (void **)device, callback); | |
caf5c03f RW |
1125 | if (ACPI_FAILURE(status) || !*device) { |
1126 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n", | |
1127 | handle)); | |
1128 | return -ENODEV; | |
1129 | } | |
1130 | return 0; | |
1131 | } | |
78ea4639 RW |
1132 | |
1133 | int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device) | |
1134 | { | |
1135 | return acpi_get_device_data(handle, device, NULL); | |
1136 | } | |
6585925b | 1137 | EXPORT_SYMBOL(acpi_bus_get_device); |
caf5c03f | 1138 | |
78ea4639 RW |
1139 | static void get_acpi_device(void *dev) |
1140 | { | |
1141 | if (dev) | |
1142 | get_device(&((struct acpi_device *)dev)->dev); | |
1143 | } | |
1144 | ||
1145 | struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle) | |
1146 | { | |
1147 | struct acpi_device *adev = NULL; | |
1148 | ||
1149 | acpi_get_device_data(handle, &adev, get_acpi_device); | |
1150 | return adev; | |
1151 | } | |
1152 | ||
1153 | void acpi_bus_put_acpi_device(struct acpi_device *adev) | |
1154 | { | |
1155 | put_device(&adev->dev); | |
1156 | } | |
1157 | ||
cf860be6 RW |
1158 | int acpi_device_add(struct acpi_device *device, |
1159 | void (*release)(struct device *)) | |
1da177e4 | 1160 | { |
4be44fcd | 1161 | int result; |
e49bd2dd ZR |
1162 | struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; |
1163 | int found = 0; | |
66b7ed40 | 1164 | |
d43e167d RW |
1165 | if (device->handle) { |
1166 | acpi_status status; | |
1167 | ||
d783156e | 1168 | status = acpi_attach_data(device->handle, acpi_scan_drop_device, |
d43e167d RW |
1169 | device); |
1170 | if (ACPI_FAILURE(status)) { | |
1171 | acpi_handle_err(device->handle, | |
1172 | "Unable to attach device data\n"); | |
1173 | return -ENODEV; | |
1174 | } | |
1175 | } | |
1176 | ||
9e89dde2 ZR |
1177 | /* |
1178 | * Linkage | |
1179 | * ------- | |
1180 | * Link this device to its parent and siblings. | |
1181 | */ | |
1182 | INIT_LIST_HEAD(&device->children); | |
1183 | INIT_LIST_HEAD(&device->node); | |
9e89dde2 | 1184 | INIT_LIST_HEAD(&device->wakeup_list); |
1033f904 | 1185 | INIT_LIST_HEAD(&device->physical_node_list); |
d783156e | 1186 | INIT_LIST_HEAD(&device->del_list); |
1033f904 | 1187 | mutex_init(&device->physical_node_lock); |
1da177e4 | 1188 | |
e49bd2dd ZR |
1189 | new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); |
1190 | if (!new_bus_id) { | |
d43e167d RW |
1191 | pr_err(PREFIX "Memory allocation error\n"); |
1192 | result = -ENOMEM; | |
1193 | goto err_detach; | |
1da177e4 | 1194 | } |
e49bd2dd | 1195 | |
9090589d | 1196 | mutex_lock(&acpi_device_lock); |
e49bd2dd ZR |
1197 | /* |
1198 | * Find suitable bus_id and instance number in acpi_bus_id_list | |
1199 | * If failed, create one and link it into acpi_bus_id_list | |
1200 | */ | |
1201 | list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { | |
1131b938 BH |
1202 | if (!strcmp(acpi_device_bus_id->bus_id, |
1203 | acpi_device_hid(device))) { | |
1204 | acpi_device_bus_id->instance_no++; | |
e49bd2dd ZR |
1205 | found = 1; |
1206 | kfree(new_bus_id); | |
1207 | break; | |
1208 | } | |
1da177e4 | 1209 | } |
0c526d96 | 1210 | if (!found) { |
e49bd2dd | 1211 | acpi_device_bus_id = new_bus_id; |
1131b938 | 1212 | strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device)); |
e49bd2dd ZR |
1213 | acpi_device_bus_id->instance_no = 0; |
1214 | list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); | |
1da177e4 | 1215 | } |
0794469d | 1216 | dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); |
1da177e4 | 1217 | |
33b57150 | 1218 | if (device->parent) |
9e89dde2 | 1219 | list_add_tail(&device->node, &device->parent->children); |
33b57150 | 1220 | |
9e89dde2 ZR |
1221 | if (device->wakeup.flags.valid) |
1222 | list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); | |
9090589d | 1223 | mutex_unlock(&acpi_device_lock); |
1da177e4 | 1224 | |
1890a97a | 1225 | if (device->parent) |
66b7ed40 | 1226 | device->dev.parent = &device->parent->dev; |
1890a97a | 1227 | device->dev.bus = &acpi_bus_type; |
82c7d5ef | 1228 | device->dev.release = release; |
cf860be6 | 1229 | result = device_add(&device->dev); |
0c526d96 | 1230 | if (result) { |
8b12b922 | 1231 | dev_err(&device->dev, "Error registering device\n"); |
d43e167d | 1232 | goto err; |
1da177e4 | 1233 | } |
1da177e4 | 1234 | |
e49bd2dd | 1235 | result = acpi_device_setup_files(device); |
0c526d96 | 1236 | if (result) |
0794469d KS |
1237 | printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n", |
1238 | dev_name(&device->dev)); | |
1da177e4 | 1239 | |
1da177e4 | 1240 | return 0; |
d43e167d RW |
1241 | |
1242 | err: | |
9090589d | 1243 | mutex_lock(&acpi_device_lock); |
33b57150 | 1244 | if (device->parent) |
e49bd2dd | 1245 | list_del(&device->node); |
e49bd2dd | 1246 | list_del(&device->wakeup_list); |
9090589d | 1247 | mutex_unlock(&acpi_device_lock); |
d43e167d RW |
1248 | |
1249 | err_detach: | |
d783156e | 1250 | acpi_detach_data(device->handle, acpi_scan_drop_device); |
e49bd2dd | 1251 | return result; |
1da177e4 LT |
1252 | } |
1253 | ||
9e89dde2 ZR |
1254 | /* -------------------------------------------------------------------------- |
1255 | Driver Management | |
1256 | -------------------------------------------------------------------------- */ | |
9e89dde2 ZR |
1257 | /** |
1258 | * acpi_bus_register_driver - register a driver with the ACPI bus | |
1259 | * @driver: driver being registered | |
1260 | * | |
1261 | * Registers a driver with the ACPI bus. Searches the namespace for all | |
1262 | * devices that match the driver's criteria and binds. Returns zero for | |
1263 | * success or a negative error status for failure. | |
1264 | */ | |
1265 | int acpi_bus_register_driver(struct acpi_driver *driver) | |
1da177e4 | 1266 | { |
1890a97a | 1267 | int ret; |
1da177e4 | 1268 | |
9e89dde2 ZR |
1269 | if (acpi_disabled) |
1270 | return -ENODEV; | |
1890a97a PM |
1271 | driver->drv.name = driver->name; |
1272 | driver->drv.bus = &acpi_bus_type; | |
1273 | driver->drv.owner = driver->owner; | |
1da177e4 | 1274 | |
1890a97a PM |
1275 | ret = driver_register(&driver->drv); |
1276 | return ret; | |
9e89dde2 | 1277 | } |
1da177e4 | 1278 | |
9e89dde2 ZR |
1279 | EXPORT_SYMBOL(acpi_bus_register_driver); |
1280 | ||
1281 | /** | |
b27b14ce | 1282 | * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus |
9e89dde2 ZR |
1283 | * @driver: driver to unregister |
1284 | * | |
1285 | * Unregisters a driver with the ACPI bus. Searches the namespace for all | |
1286 | * devices that match the driver's criteria and unbinds. | |
1287 | */ | |
1288 | void acpi_bus_unregister_driver(struct acpi_driver *driver) | |
1289 | { | |
1890a97a | 1290 | driver_unregister(&driver->drv); |
9e89dde2 ZR |
1291 | } |
1292 | ||
1293 | EXPORT_SYMBOL(acpi_bus_unregister_driver); | |
1294 | ||
9e89dde2 ZR |
1295 | /* -------------------------------------------------------------------------- |
1296 | Device Enumeration | |
1297 | -------------------------------------------------------------------------- */ | |
5c478f49 BH |
1298 | static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) |
1299 | { | |
456de893 | 1300 | struct acpi_device *device = NULL; |
5c478f49 | 1301 | acpi_status status; |
5c478f49 BH |
1302 | |
1303 | /* | |
1304 | * Fixed hardware devices do not appear in the namespace and do not | |
1305 | * have handles, but we fabricate acpi_devices for them, so we have | |
1306 | * to deal with them specially. | |
1307 | */ | |
456de893 | 1308 | if (!handle) |
5c478f49 BH |
1309 | return acpi_root; |
1310 | ||
1311 | do { | |
1312 | status = acpi_get_parent(handle, &handle); | |
5c478f49 | 1313 | if (ACPI_FAILURE(status)) |
456de893 RW |
1314 | return status == AE_NULL_ENTRY ? NULL : acpi_root; |
1315 | } while (acpi_bus_get_device(handle, &device)); | |
1316 | return device; | |
5c478f49 BH |
1317 | } |
1318 | ||
9e89dde2 ZR |
1319 | acpi_status |
1320 | acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) | |
1321 | { | |
1322 | acpi_status status; | |
1323 | acpi_handle tmp; | |
1324 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; | |
1325 | union acpi_object *obj; | |
1326 | ||
1327 | status = acpi_get_handle(handle, "_EJD", &tmp); | |
1328 | if (ACPI_FAILURE(status)) | |
1329 | return status; | |
1330 | ||
1331 | status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); | |
1332 | if (ACPI_SUCCESS(status)) { | |
1333 | obj = buffer.pointer; | |
3b5fee59 HM |
1334 | status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, |
1335 | ejd); | |
9e89dde2 ZR |
1336 | kfree(buffer.pointer); |
1337 | } | |
1338 | return status; | |
1339 | } | |
1340 | EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); | |
1341 | ||
e88c9c60 RW |
1342 | static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, |
1343 | struct acpi_device_wakeup *wakeup) | |
9e89dde2 | 1344 | { |
b581a7f9 RW |
1345 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
1346 | union acpi_object *package = NULL; | |
9e89dde2 | 1347 | union acpi_object *element = NULL; |
b581a7f9 | 1348 | acpi_status status; |
e88c9c60 | 1349 | int err = -ENODATA; |
9e89dde2 | 1350 | |
b581a7f9 | 1351 | if (!wakeup) |
e88c9c60 | 1352 | return -EINVAL; |
9e89dde2 | 1353 | |
993cbe59 | 1354 | INIT_LIST_HEAD(&wakeup->resources); |
9e89dde2 | 1355 | |
b581a7f9 RW |
1356 | /* _PRW */ |
1357 | status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); | |
1358 | if (ACPI_FAILURE(status)) { | |
1359 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); | |
e88c9c60 | 1360 | return err; |
b581a7f9 RW |
1361 | } |
1362 | ||
1363 | package = (union acpi_object *)buffer.pointer; | |
1364 | ||
e88c9c60 | 1365 | if (!package || package->package.count < 2) |
b581a7f9 | 1366 | goto out; |
b581a7f9 | 1367 | |
9e89dde2 | 1368 | element = &(package->package.elements[0]); |
e88c9c60 | 1369 | if (!element) |
b581a7f9 | 1370 | goto out; |
e88c9c60 | 1371 | |
9e89dde2 ZR |
1372 | if (element->type == ACPI_TYPE_PACKAGE) { |
1373 | if ((element->package.count < 2) || | |
1374 | (element->package.elements[0].type != | |
1375 | ACPI_TYPE_LOCAL_REFERENCE) | |
e88c9c60 | 1376 | || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) |
b581a7f9 | 1377 | goto out; |
e88c9c60 | 1378 | |
b581a7f9 | 1379 | wakeup->gpe_device = |
9e89dde2 | 1380 | element->package.elements[0].reference.handle; |
b581a7f9 | 1381 | wakeup->gpe_number = |
9e89dde2 ZR |
1382 | (u32) element->package.elements[1].integer.value; |
1383 | } else if (element->type == ACPI_TYPE_INTEGER) { | |
b581a7f9 RW |
1384 | wakeup->gpe_device = NULL; |
1385 | wakeup->gpe_number = element->integer.value; | |
1386 | } else { | |
b581a7f9 RW |
1387 | goto out; |
1388 | } | |
9e89dde2 ZR |
1389 | |
1390 | element = &(package->package.elements[1]); | |
e88c9c60 | 1391 | if (element->type != ACPI_TYPE_INTEGER) |
b581a7f9 | 1392 | goto out; |
e88c9c60 | 1393 | |
b581a7f9 | 1394 | wakeup->sleep_state = element->integer.value; |
9e89dde2 | 1395 | |
e88c9c60 RW |
1396 | err = acpi_extract_power_resources(package, 2, &wakeup->resources); |
1397 | if (err) | |
b581a7f9 | 1398 | goto out; |
9e89dde2 | 1399 | |
0596a52b RW |
1400 | if (!list_empty(&wakeup->resources)) { |
1401 | int sleep_state; | |
9e89dde2 | 1402 | |
b5d667eb RW |
1403 | err = acpi_power_wakeup_list_init(&wakeup->resources, |
1404 | &sleep_state); | |
1405 | if (err) { | |
1406 | acpi_handle_warn(handle, "Retrieving current states " | |
1407 | "of wakeup power resources failed\n"); | |
1408 | acpi_power_resources_list_free(&wakeup->resources); | |
1409 | goto out; | |
1410 | } | |
0596a52b RW |
1411 | if (sleep_state < wakeup->sleep_state) { |
1412 | acpi_handle_warn(handle, "Overriding _PRW sleep state " | |
1413 | "(S%d) by S%d from power resources\n", | |
1414 | (int)wakeup->sleep_state, sleep_state); | |
1415 | wakeup->sleep_state = sleep_state; | |
1416 | } | |
1417 | } | |
bba63a29 | 1418 | acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); |
9874647b | 1419 | |
b581a7f9 RW |
1420 | out: |
1421 | kfree(buffer.pointer); | |
e88c9c60 | 1422 | return err; |
1da177e4 LT |
1423 | } |
1424 | ||
f517709d | 1425 | static void acpi_bus_set_run_wake_flags(struct acpi_device *device) |
1da177e4 | 1426 | { |
29b71a1c | 1427 | struct acpi_device_id button_device_ids[] = { |
29b71a1c | 1428 | {"PNP0C0C", 0}, |
b7e38304 | 1429 | {"PNP0C0D", 0}, |
29b71a1c TR |
1430 | {"PNP0C0E", 0}, |
1431 | {"", 0}, | |
1432 | }; | |
f517709d RW |
1433 | acpi_status status; |
1434 | acpi_event_status event_status; | |
1435 | ||
b67ea761 | 1436 | device->wakeup.flags.notifier_present = 0; |
f517709d RW |
1437 | |
1438 | /* Power button, Lid switch always enable wakeup */ | |
1439 | if (!acpi_match_device_ids(device, button_device_ids)) { | |
1440 | device->wakeup.flags.run_wake = 1; | |
b7e38304 ZR |
1441 | if (!acpi_match_device_ids(device, &button_device_ids[1])) { |
1442 | /* Do not use Lid/sleep button for S5 wakeup */ | |
1443 | if (device->wakeup.sleep_state == ACPI_STATE_S5) | |
1444 | device->wakeup.sleep_state = ACPI_STATE_S4; | |
1445 | } | |
f2b56bc8 | 1446 | device_set_wakeup_capable(&device->dev, true); |
f517709d RW |
1447 | return; |
1448 | } | |
1449 | ||
e8e18c95 RW |
1450 | status = acpi_get_gpe_status(device->wakeup.gpe_device, |
1451 | device->wakeup.gpe_number, | |
1452 | &event_status); | |
f517709d RW |
1453 | if (status == AE_OK) |
1454 | device->wakeup.flags.run_wake = | |
1455 | !!(event_status & ACPI_EVENT_FLAG_HANDLE); | |
1456 | } | |
1457 | ||
d57d09a4 | 1458 | static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) |
f517709d | 1459 | { |
e88c9c60 | 1460 | int err; |
29b71a1c | 1461 | |
d57d09a4 | 1462 | /* Presence of _PRW indicates wake capable */ |
952c63e9 | 1463 | if (!acpi_has_method(device->handle, "_PRW")) |
d57d09a4 RW |
1464 | return; |
1465 | ||
e88c9c60 RW |
1466 | err = acpi_bus_extract_wakeup_device_power_package(device->handle, |
1467 | &device->wakeup); | |
1468 | if (err) { | |
1469 | dev_err(&device->dev, "_PRW evaluation error: %d\n", err); | |
d57d09a4 | 1470 | return; |
9e89dde2 | 1471 | } |
1da177e4 | 1472 | |
9e89dde2 | 1473 | device->wakeup.flags.valid = 1; |
9b83ccd2 | 1474 | device->wakeup.prepare_count = 0; |
f517709d | 1475 | acpi_bus_set_run_wake_flags(device); |
729b2bdb ZY |
1476 | /* Call _PSW/_DSW object to disable its ability to wake the sleeping |
1477 | * system for the ACPI device with the _PRW object. | |
1478 | * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW. | |
1479 | * So it is necessary to call _DSW object first. Only when it is not | |
1480 | * present will the _PSW object used. | |
1481 | */ | |
e88c9c60 RW |
1482 | err = acpi_device_sleep_wake(device, 0, 0, 0); |
1483 | if (err) | |
77e76609 RW |
1484 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
1485 | "error in _DSW or _PSW evaluation\n")); | |
1da177e4 | 1486 | } |
1da177e4 | 1487 | |
f33ce568 RW |
1488 | static void acpi_bus_init_power_state(struct acpi_device *device, int state) |
1489 | { | |
1490 | struct acpi_device_power_state *ps = &device->power.states[state]; | |
ef85bdbe RW |
1491 | char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' }; |
1492 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
f33ce568 | 1493 | acpi_status status; |
bf325f95 | 1494 | |
f33ce568 RW |
1495 | INIT_LIST_HEAD(&ps->resources); |
1496 | ||
ef85bdbe RW |
1497 | /* Evaluate "_PRx" to get referenced power resources */ |
1498 | status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer); | |
1499 | if (ACPI_SUCCESS(status)) { | |
1500 | union acpi_object *package = buffer.pointer; | |
1501 | ||
1502 | if (buffer.length && package | |
1503 | && package->type == ACPI_TYPE_PACKAGE | |
1504 | && package->package.count) { | |
e88c9c60 RW |
1505 | int err = acpi_extract_power_resources(package, 0, |
1506 | &ps->resources); | |
1507 | if (!err) | |
ef85bdbe | 1508 | device->power.flags.power_resources = 1; |
f33ce568 | 1509 | } |
ef85bdbe | 1510 | ACPI_FREE(buffer.pointer); |
f33ce568 RW |
1511 | } |
1512 | ||
1513 | /* Evaluate "_PSx" to see if we can do explicit sets */ | |
ef85bdbe | 1514 | pathname[2] = 'S'; |
952c63e9 | 1515 | if (acpi_has_method(device->handle, pathname)) |
f33ce568 RW |
1516 | ps->flags.explicit_set = 1; |
1517 | ||
1518 | /* | |
1519 | * State is valid if there are means to put the device into it. | |
1520 | * D3hot is only valid if _PR3 present. | |
1521 | */ | |
ef85bdbe | 1522 | if (!list_empty(&ps->resources) |
f33ce568 RW |
1523 | || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) { |
1524 | ps->flags.valid = 1; | |
1525 | ps->flags.os_accessible = 1; | |
1526 | } | |
1527 | ||
1528 | ps->power = -1; /* Unknown - driver assigned */ | |
1529 | ps->latency = -1; /* Unknown - driver assigned */ | |
1530 | } | |
1531 | ||
d43e167d | 1532 | static void acpi_bus_get_power_flags(struct acpi_device *device) |
1da177e4 | 1533 | { |
8bc5053b | 1534 | u32 i; |
1a365616 | 1535 | |
d43e167d | 1536 | /* Presence of _PS0|_PR0 indicates 'power manageable' */ |
952c63e9 JL |
1537 | if (!acpi_has_method(device->handle, "_PS0") && |
1538 | !acpi_has_method(device->handle, "_PR0")) | |
1539 | return; | |
1a365616 | 1540 | |
d43e167d | 1541 | device->flags.power_manageable = 1; |
4be44fcd | 1542 | |
9e89dde2 ZR |
1543 | /* |
1544 | * Power Management Flags | |
1545 | */ | |
952c63e9 | 1546 | if (acpi_has_method(device->handle, "_PSC")) |
9e89dde2 | 1547 | device->power.flags.explicit_get = 1; |
952c63e9 | 1548 | if (acpi_has_method(device->handle, "_IRC")) |
9e89dde2 | 1549 | device->power.flags.inrush_current = 1; |
1da177e4 | 1550 | |
9e89dde2 ZR |
1551 | /* |
1552 | * Enumerate supported power management states | |
1553 | */ | |
f33ce568 RW |
1554 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) |
1555 | acpi_bus_init_power_state(device, i); | |
bf325f95 | 1556 | |
0b224527 | 1557 | INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); |
1da177e4 | 1558 | |
9e89dde2 ZR |
1559 | /* Set defaults for D0 and D3 states (always valid) */ |
1560 | device->power.states[ACPI_STATE_D0].flags.valid = 1; | |
1561 | device->power.states[ACPI_STATE_D0].power = 100; | |
8ad928d5 RW |
1562 | device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1; |
1563 | device->power.states[ACPI_STATE_D3_COLD].power = 0; | |
c8f7a62c | 1564 | |
5c7dd710 RW |
1565 | /* Set D3cold's explicit_set flag if _PS3 exists. */ |
1566 | if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set) | |
1567 | device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1; | |
1568 | ||
1399dfcd AL |
1569 | /* Presence of _PS3 or _PRx means we can put the device into D3 cold */ |
1570 | if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set || | |
1571 | device->power.flags.power_resources) | |
1572 | device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1; | |
1573 | ||
b3785492 RW |
1574 | if (acpi_bus_init_power(device)) { |
1575 | acpi_free_power_resources_lists(device); | |
1576 | device->flags.power_manageable = 0; | |
1577 | } | |
9e89dde2 | 1578 | } |
c8f7a62c | 1579 | |
d43e167d | 1580 | static void acpi_bus_get_flags(struct acpi_device *device) |
1da177e4 | 1581 | { |
1da177e4 | 1582 | /* Presence of _STA indicates 'dynamic_status' */ |
952c63e9 | 1583 | if (acpi_has_method(device->handle, "_STA")) |
1da177e4 LT |
1584 | device->flags.dynamic_status = 1; |
1585 | ||
1da177e4 | 1586 | /* Presence of _RMV indicates 'removable' */ |
952c63e9 | 1587 | if (acpi_has_method(device->handle, "_RMV")) |
1da177e4 LT |
1588 | device->flags.removable = 1; |
1589 | ||
1590 | /* Presence of _EJD|_EJ0 indicates 'ejectable' */ | |
952c63e9 JL |
1591 | if (acpi_has_method(device->handle, "_EJD") || |
1592 | acpi_has_method(device->handle, "_EJ0")) | |
1da177e4 | 1593 | device->flags.ejectable = 1; |
1da177e4 LT |
1594 | } |
1595 | ||
c7bcb4e9 | 1596 | static void acpi_device_get_busid(struct acpi_device *device) |
1da177e4 | 1597 | { |
4be44fcd LB |
1598 | char bus_id[5] = { '?', 0 }; |
1599 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; | |
1600 | int i = 0; | |
1da177e4 LT |
1601 | |
1602 | /* | |
1603 | * Bus ID | |
1604 | * ------ | |
1605 | * The device's Bus ID is simply the object name. | |
1606 | * TBD: Shouldn't this value be unique (within the ACPI namespace)? | |
1607 | */ | |
859ac9a4 | 1608 | if (ACPI_IS_ROOT_DEVICE(device)) { |
1da177e4 | 1609 | strcpy(device->pnp.bus_id, "ACPI"); |
859ac9a4 BH |
1610 | return; |
1611 | } | |
1612 | ||
1613 | switch (device->device_type) { | |
1da177e4 LT |
1614 | case ACPI_BUS_TYPE_POWER_BUTTON: |
1615 | strcpy(device->pnp.bus_id, "PWRF"); | |
1616 | break; | |
1617 | case ACPI_BUS_TYPE_SLEEP_BUTTON: | |
1618 | strcpy(device->pnp.bus_id, "SLPF"); | |
1619 | break; | |
1620 | default: | |
66b7ed40 | 1621 | acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); |
1da177e4 LT |
1622 | /* Clean up trailing underscores (if any) */ |
1623 | for (i = 3; i > 1; i--) { | |
1624 | if (bus_id[i] == '_') | |
1625 | bus_id[i] = '\0'; | |
1626 | else | |
1627 | break; | |
1628 | } | |
1629 | strcpy(device->pnp.bus_id, bus_id); | |
1630 | break; | |
1631 | } | |
1632 | } | |
1633 | ||
ebf4df8d JL |
1634 | /* |
1635 | * acpi_ata_match - see if an acpi object is an ATA device | |
1636 | * | |
1637 | * If an acpi object has one of the ACPI ATA methods defined, | |
1638 | * then we can safely call it an ATA device. | |
1639 | */ | |
1640 | bool acpi_ata_match(acpi_handle handle) | |
1641 | { | |
1642 | return acpi_has_method(handle, "_GTF") || | |
1643 | acpi_has_method(handle, "_GTM") || | |
1644 | acpi_has_method(handle, "_STM") || | |
1645 | acpi_has_method(handle, "_SDD"); | |
1646 | } | |
1647 | ||
54735266 | 1648 | /* |
d4e1a692 | 1649 | * acpi_bay_match - see if an acpi object is an ejectable driver bay |
54735266 ZR |
1650 | * |
1651 | * If an acpi object is ejectable and has one of the ACPI ATA methods defined, | |
1652 | * then we can safely call it an ejectable drive bay | |
1653 | */ | |
ebf4df8d | 1654 | bool acpi_bay_match(acpi_handle handle) |
d4e1a692 | 1655 | { |
54735266 ZR |
1656 | acpi_handle phandle; |
1657 | ||
952c63e9 | 1658 | if (!acpi_has_method(handle, "_EJ0")) |
ebf4df8d JL |
1659 | return false; |
1660 | if (acpi_ata_match(handle)) | |
1661 | return true; | |
1662 | if (ACPI_FAILURE(acpi_get_parent(handle, &phandle))) | |
1663 | return false; | |
54735266 | 1664 | |
ebf4df8d | 1665 | return acpi_ata_match(phandle); |
54735266 ZR |
1666 | } |
1667 | ||
3620f2f2 | 1668 | /* |
d4e1a692 | 1669 | * acpi_dock_match - see if an acpi object has a _DCK method |
3620f2f2 | 1670 | */ |
ebf4df8d | 1671 | bool acpi_dock_match(acpi_handle handle) |
3620f2f2 | 1672 | { |
ebf4df8d | 1673 | return acpi_has_method(handle, "_DCK"); |
3620f2f2 FS |
1674 | } |
1675 | ||
620e112c | 1676 | const char *acpi_device_hid(struct acpi_device *device) |
15b8dd53 | 1677 | { |
7f47fa6c | 1678 | struct acpi_hardware_id *hid; |
15b8dd53 | 1679 | |
2b2ae7c7 TR |
1680 | if (list_empty(&device->pnp.ids)) |
1681 | return dummy_hid; | |
1682 | ||
7f47fa6c BH |
1683 | hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list); |
1684 | return hid->id; | |
1685 | } | |
1686 | EXPORT_SYMBOL(acpi_device_hid); | |
15b8dd53 | 1687 | |
d4e1a692 | 1688 | static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id) |
7f47fa6c BH |
1689 | { |
1690 | struct acpi_hardware_id *id; | |
15b8dd53 | 1691 | |
7f47fa6c BH |
1692 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
1693 | if (!id) | |
1694 | return; | |
15b8dd53 | 1695 | |
581de59e | 1696 | id->id = kstrdup(dev_id, GFP_KERNEL); |
7f47fa6c BH |
1697 | if (!id->id) { |
1698 | kfree(id); | |
1699 | return; | |
15b8dd53 BM |
1700 | } |
1701 | ||
d4e1a692 TK |
1702 | list_add_tail(&id->list, &pnp->ids); |
1703 | pnp->type.hardware_id = 1; | |
15b8dd53 BM |
1704 | } |
1705 | ||
222e82ac DW |
1706 | /* |
1707 | * Old IBM workstations have a DSDT bug wherein the SMBus object | |
1708 | * lacks the SMBUS01 HID and the methods do not have the necessary "_" | |
1709 | * prefix. Work around this. | |
1710 | */ | |
ebf4df8d | 1711 | static bool acpi_ibm_smbus_match(acpi_handle handle) |
222e82ac | 1712 | { |
ebf4df8d JL |
1713 | char node_name[ACPI_PATH_SEGMENT_LENGTH]; |
1714 | struct acpi_buffer path = { sizeof(node_name), node_name }; | |
222e82ac DW |
1715 | |
1716 | if (!dmi_name_in_vendors("IBM")) | |
ebf4df8d | 1717 | return false; |
222e82ac DW |
1718 | |
1719 | /* Look for SMBS object */ | |
ebf4df8d JL |
1720 | if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) || |
1721 | strcmp("SMBS", path.pointer)) | |
1722 | return false; | |
222e82ac DW |
1723 | |
1724 | /* Does it have the necessary (but misnamed) methods? */ | |
952c63e9 JL |
1725 | if (acpi_has_method(handle, "SBI") && |
1726 | acpi_has_method(handle, "SBR") && | |
1727 | acpi_has_method(handle, "SBW")) | |
ebf4df8d JL |
1728 | return true; |
1729 | ||
1730 | return false; | |
222e82ac DW |
1731 | } |
1732 | ||
c0af4175 TK |
1733 | static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, |
1734 | int device_type) | |
1da177e4 | 1735 | { |
4be44fcd | 1736 | acpi_status status; |
57f3674f | 1737 | struct acpi_device_info *info; |
78e25fef | 1738 | struct acpi_pnp_device_id_list *cid_list; |
7f47fa6c | 1739 | int i; |
1da177e4 | 1740 | |
c0af4175 | 1741 | switch (device_type) { |
1da177e4 | 1742 | case ACPI_BUS_TYPE_DEVICE: |
c0af4175 TK |
1743 | if (handle == ACPI_ROOT_OBJECT) { |
1744 | acpi_add_id(pnp, ACPI_SYSTEM_HID); | |
859ac9a4 BH |
1745 | break; |
1746 | } | |
1747 | ||
c0af4175 | 1748 | status = acpi_get_object_info(handle, &info); |
1da177e4 | 1749 | if (ACPI_FAILURE(status)) { |
c0af4175 TK |
1750 | pr_err(PREFIX "%s: Error reading device info\n", |
1751 | __func__); | |
1da177e4 LT |
1752 | return; |
1753 | } | |
1754 | ||
1da177e4 | 1755 | if (info->valid & ACPI_VALID_HID) |
c0af4175 | 1756 | acpi_add_id(pnp, info->hardware_id.string); |
57f3674f | 1757 | if (info->valid & ACPI_VALID_CID) { |
15b8dd53 | 1758 | cid_list = &info->compatible_id_list; |
57f3674f | 1759 | for (i = 0; i < cid_list->count; i++) |
c0af4175 | 1760 | acpi_add_id(pnp, cid_list->ids[i].string); |
57f3674f | 1761 | } |
1da177e4 | 1762 | if (info->valid & ACPI_VALID_ADR) { |
c0af4175 TK |
1763 | pnp->bus_address = info->address; |
1764 | pnp->type.bus_address = 1; | |
1da177e4 | 1765 | } |
ccf78040 | 1766 | if (info->valid & ACPI_VALID_UID) |
c0af4175 | 1767 | pnp->unique_id = kstrdup(info->unique_id.string, |
ccf78040 | 1768 | GFP_KERNEL); |
ae843332 | 1769 | |
a83893ae BH |
1770 | kfree(info); |
1771 | ||
57f3674f BH |
1772 | /* |
1773 | * Some devices don't reliably have _HIDs & _CIDs, so add | |
1774 | * synthetic HIDs to make sure drivers can find them. | |
1775 | */ | |
c0af4175 TK |
1776 | if (acpi_is_video_device(handle)) |
1777 | acpi_add_id(pnp, ACPI_VIDEO_HID); | |
ebf4df8d | 1778 | else if (acpi_bay_match(handle)) |
c0af4175 | 1779 | acpi_add_id(pnp, ACPI_BAY_HID); |
ebf4df8d | 1780 | else if (acpi_dock_match(handle)) |
c0af4175 | 1781 | acpi_add_id(pnp, ACPI_DOCK_HID); |
ebf4df8d | 1782 | else if (acpi_ibm_smbus_match(handle)) |
c0af4175 TK |
1783 | acpi_add_id(pnp, ACPI_SMBUS_IBM_HID); |
1784 | else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) { | |
1785 | acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */ | |
1786 | strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME); | |
1787 | strcpy(pnp->device_class, ACPI_BUS_CLASS); | |
b7b30de5 | 1788 | } |
54735266 | 1789 | |
1da177e4 LT |
1790 | break; |
1791 | case ACPI_BUS_TYPE_POWER: | |
c0af4175 | 1792 | acpi_add_id(pnp, ACPI_POWER_HID); |
1da177e4 LT |
1793 | break; |
1794 | case ACPI_BUS_TYPE_PROCESSOR: | |
c0af4175 | 1795 | acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID); |
1da177e4 | 1796 | break; |
1da177e4 | 1797 | case ACPI_BUS_TYPE_THERMAL: |
c0af4175 | 1798 | acpi_add_id(pnp, ACPI_THERMAL_HID); |
1da177e4 LT |
1799 | break; |
1800 | case ACPI_BUS_TYPE_POWER_BUTTON: | |
c0af4175 | 1801 | acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF); |
1da177e4 LT |
1802 | break; |
1803 | case ACPI_BUS_TYPE_SLEEP_BUTTON: | |
c0af4175 | 1804 | acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF); |
1da177e4 LT |
1805 | break; |
1806 | } | |
1da177e4 LT |
1807 | } |
1808 | ||
c0af4175 TK |
1809 | void acpi_free_pnp_ids(struct acpi_device_pnp *pnp) |
1810 | { | |
1811 | struct acpi_hardware_id *id, *tmp; | |
1812 | ||
1813 | list_for_each_entry_safe(id, tmp, &pnp->ids, list) { | |
1814 | kfree(id->id); | |
1815 | kfree(id); | |
1816 | } | |
1817 | kfree(pnp->unique_id); | |
1818 | } | |
1819 | ||
82c7d5ef RW |
1820 | void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, |
1821 | int type, unsigned long long sta) | |
1da177e4 | 1822 | { |
d43e167d RW |
1823 | INIT_LIST_HEAD(&device->pnp.ids); |
1824 | device->device_type = type; | |
1825 | device->handle = handle; | |
1826 | device->parent = acpi_bus_get_parent(handle); | |
25db115b | 1827 | acpi_set_device_status(device, sta); |
d43e167d | 1828 | acpi_device_get_busid(device); |
c0af4175 | 1829 | acpi_set_pnp_ids(handle, &device->pnp, type); |
d43e167d | 1830 | acpi_bus_get_flags(device); |
2c0d4fe0 | 1831 | device->flags.match_driver = false; |
202317a5 RW |
1832 | device->flags.initialized = true; |
1833 | device->flags.visited = false; | |
cf860be6 RW |
1834 | device_initialize(&device->dev); |
1835 | dev_set_uevent_suppress(&device->dev, true); | |
1836 | } | |
bc3b0772 | 1837 | |
cf860be6 RW |
1838 | void acpi_device_add_finalize(struct acpi_device *device) |
1839 | { | |
1840 | dev_set_uevent_suppress(&device->dev, false); | |
1841 | kobject_uevent(&device->dev.kobj, KOBJ_ADD); | |
1da177e4 LT |
1842 | } |
1843 | ||
5c478f49 BH |
1844 | static int acpi_add_single_object(struct acpi_device **child, |
1845 | acpi_handle handle, int type, | |
2c0d4fe0 | 1846 | unsigned long long sta) |
1da177e4 | 1847 | { |
77c24888 BH |
1848 | int result; |
1849 | struct acpi_device *device; | |
29aaefa6 | 1850 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
1da177e4 | 1851 | |
36bcbec7 | 1852 | device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); |
1da177e4 | 1853 | if (!device) { |
6468463a | 1854 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
d550d98d | 1855 | return -ENOMEM; |
1da177e4 | 1856 | } |
1da177e4 | 1857 | |
d43e167d RW |
1858 | acpi_init_device_object(device, handle, type, sta); |
1859 | acpi_bus_get_power_flags(device); | |
d57d09a4 | 1860 | acpi_bus_get_wakeup_device_flags(device); |
1da177e4 | 1861 | |
cf860be6 | 1862 | result = acpi_device_add(device, acpi_device_release); |
d43e167d | 1863 | if (result) { |
718fb0de | 1864 | acpi_device_release(&device->dev); |
d43e167d RW |
1865 | return result; |
1866 | } | |
1da177e4 | 1867 | |
d43e167d | 1868 | acpi_power_add_remove_device(device, true); |
cf860be6 | 1869 | acpi_device_add_finalize(device); |
d43e167d RW |
1870 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
1871 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n", | |
1872 | dev_name(&device->dev), (char *) buffer.pointer, | |
1873 | device->parent ? dev_name(&device->parent->dev) : "(null)")); | |
1874 | kfree(buffer.pointer); | |
1875 | *child = device; | |
1876 | return 0; | |
bf325f95 RW |
1877 | } |
1878 | ||
778cbc1d BH |
1879 | static int acpi_bus_type_and_status(acpi_handle handle, int *type, |
1880 | unsigned long long *sta) | |
1da177e4 | 1881 | { |
778cbc1d BH |
1882 | acpi_status status; |
1883 | acpi_object_type acpi_type; | |
1da177e4 | 1884 | |
778cbc1d | 1885 | status = acpi_get_type(handle, &acpi_type); |
51a85faf | 1886 | if (ACPI_FAILURE(status)) |
778cbc1d | 1887 | return -ENODEV; |
4be44fcd | 1888 | |
778cbc1d | 1889 | switch (acpi_type) { |
51a85faf BH |
1890 | case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ |
1891 | case ACPI_TYPE_DEVICE: | |
778cbc1d BH |
1892 | *type = ACPI_BUS_TYPE_DEVICE; |
1893 | status = acpi_bus_get_status_handle(handle, sta); | |
1894 | if (ACPI_FAILURE(status)) | |
1895 | return -ENODEV; | |
51a85faf BH |
1896 | break; |
1897 | case ACPI_TYPE_PROCESSOR: | |
778cbc1d BH |
1898 | *type = ACPI_BUS_TYPE_PROCESSOR; |
1899 | status = acpi_bus_get_status_handle(handle, sta); | |
1900 | if (ACPI_FAILURE(status)) | |
1901 | return -ENODEV; | |
51a85faf BH |
1902 | break; |
1903 | case ACPI_TYPE_THERMAL: | |
778cbc1d BH |
1904 | *type = ACPI_BUS_TYPE_THERMAL; |
1905 | *sta = ACPI_STA_DEFAULT; | |
51a85faf BH |
1906 | break; |
1907 | case ACPI_TYPE_POWER: | |
778cbc1d BH |
1908 | *type = ACPI_BUS_TYPE_POWER; |
1909 | *sta = ACPI_STA_DEFAULT; | |
51a85faf BH |
1910 | break; |
1911 | default: | |
778cbc1d | 1912 | return -ENODEV; |
51a85faf | 1913 | } |
1da177e4 | 1914 | |
778cbc1d BH |
1915 | return 0; |
1916 | } | |
1917 | ||
202317a5 RW |
1918 | bool acpi_device_is_present(struct acpi_device *adev) |
1919 | { | |
1920 | if (adev->status.present || adev->status.functional) | |
1921 | return true; | |
1922 | ||
1923 | adev->flags.initialized = false; | |
1924 | return false; | |
1925 | } | |
1926 | ||
4b59cc1f RW |
1927 | static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler, |
1928 | char *idstr, | |
1929 | const struct acpi_device_id **matchid) | |
1930 | { | |
1931 | const struct acpi_device_id *devid; | |
1932 | ||
1933 | for (devid = handler->ids; devid->id[0]; devid++) | |
1934 | if (!strcmp((char *)devid->id, idstr)) { | |
1935 | if (matchid) | |
1936 | *matchid = devid; | |
1937 | ||
1938 | return true; | |
1939 | } | |
1940 | ||
1941 | return false; | |
1942 | } | |
1943 | ||
6b772e8f TK |
1944 | static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr, |
1945 | const struct acpi_device_id **matchid) | |
1946 | { | |
1947 | struct acpi_scan_handler *handler; | |
1948 | ||
1949 | list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) | |
1950 | if (acpi_scan_handler_matching(handler, idstr, matchid)) | |
1951 | return handler; | |
1952 | ||
1953 | return NULL; | |
1954 | } | |
1955 | ||
3f8055c3 RW |
1956 | void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val) |
1957 | { | |
3f8055c3 RW |
1958 | if (!!hotplug->enabled == !!val) |
1959 | return; | |
1960 | ||
1961 | mutex_lock(&acpi_scan_lock); | |
1962 | ||
1963 | hotplug->enabled = val; | |
3f8055c3 RW |
1964 | |
1965 | mutex_unlock(&acpi_scan_lock); | |
1966 | } | |
1967 | ||
6b772e8f | 1968 | static void acpi_scan_init_hotplug(acpi_handle handle, int type) |
a33ec399 | 1969 | { |
6b772e8f TK |
1970 | struct acpi_device_pnp pnp = {}; |
1971 | struct acpi_hardware_id *hwid; | |
a33ec399 RW |
1972 | struct acpi_scan_handler *handler; |
1973 | ||
6b772e8f TK |
1974 | INIT_LIST_HEAD(&pnp.ids); |
1975 | acpi_set_pnp_ids(handle, &pnp, type); | |
a33ec399 | 1976 | |
6b772e8f | 1977 | if (!pnp.type.hardware_id) |
7a26b530 | 1978 | goto out; |
a33ec399 RW |
1979 | |
1980 | /* | |
1981 | * This relies on the fact that acpi_install_notify_handler() will not | |
1982 | * install the same notify handler routine twice for the same handle. | |
1983 | */ | |
6b772e8f TK |
1984 | list_for_each_entry(hwid, &pnp.ids, list) { |
1985 | handler = acpi_scan_match_handler(hwid->id, NULL); | |
3338db00 | 1986 | if (handler) { |
2cbb14fb TK |
1987 | acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
1988 | acpi_hotplug_notify_cb, handler); | |
6b772e8f TK |
1989 | break; |
1990 | } | |
1991 | } | |
1992 | ||
7a26b530 | 1993 | out: |
6b772e8f | 1994 | acpi_free_pnp_ids(&pnp); |
a33ec399 RW |
1995 | } |
1996 | ||
e3863094 RW |
1997 | static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, |
1998 | void *not_used, void **return_value) | |
778cbc1d | 1999 | { |
805d410f | 2000 | struct acpi_device *device = NULL; |
778cbc1d BH |
2001 | int type; |
2002 | unsigned long long sta; | |
2003 | int result; | |
2004 | ||
4002bf38 RW |
2005 | acpi_bus_get_device(handle, &device); |
2006 | if (device) | |
2007 | goto out; | |
2008 | ||
778cbc1d BH |
2009 | result = acpi_bus_type_and_status(handle, &type, &sta); |
2010 | if (result) | |
2011 | return AE_OK; | |
2012 | ||
82c7d5ef RW |
2013 | if (type == ACPI_BUS_TYPE_POWER) { |
2014 | acpi_add_power_resource(handle); | |
2015 | return AE_OK; | |
2016 | } | |
2017 | ||
6b772e8f | 2018 | acpi_scan_init_hotplug(handle, type); |
a33ec399 | 2019 | |
2c0d4fe0 | 2020 | acpi_add_single_object(&device, handle, type, sta); |
e3b87f8a | 2021 | if (!device) |
51a85faf | 2022 | return AE_CTRL_DEPTH; |
1da177e4 | 2023 | |
4002bf38 | 2024 | out: |
51a85faf BH |
2025 | if (!*return_value) |
2026 | *return_value = device; | |
805d410f | 2027 | |
51a85faf BH |
2028 | return AE_OK; |
2029 | } | |
3fb02738 | 2030 | |
c5698074 | 2031 | static int acpi_scan_attach_handler(struct acpi_device *device) |
ca589f94 | 2032 | { |
c5698074 RW |
2033 | struct acpi_hardware_id *hwid; |
2034 | int ret = 0; | |
ca589f94 | 2035 | |
c5698074 | 2036 | list_for_each_entry(hwid, &device->pnp.ids, list) { |
87b85b3c | 2037 | const struct acpi_device_id *devid; |
c5698074 | 2038 | struct acpi_scan_handler *handler; |
ca589f94 | 2039 | |
c5698074 RW |
2040 | handler = acpi_scan_match_handler(hwid->id, &devid); |
2041 | if (handler) { | |
87b85b3c RW |
2042 | ret = handler->attach(device, devid); |
2043 | if (ret > 0) { | |
2044 | device->handler = handler; | |
c5698074 | 2045 | break; |
87b85b3c | 2046 | } else if (ret < 0) { |
c5698074 | 2047 | break; |
87b85b3c | 2048 | } |
ca589f94 RW |
2049 | } |
2050 | } | |
2051 | return ret; | |
2052 | } | |
2053 | ||
2c22e652 | 2054 | static void acpi_bus_attach(struct acpi_device *device) |
51a85faf | 2055 | { |
2c22e652 | 2056 | struct acpi_device *child; |
ca589f94 | 2057 | int ret; |
3fb02738 | 2058 | |
2c22e652 | 2059 | acpi_bus_get_status(device); |
202317a5 | 2060 | /* Skip devices that are not present. */ |
2c22e652 RW |
2061 | if (!acpi_device_is_present(device)) { |
2062 | device->flags.visited = false; | |
2063 | return; | |
2064 | } | |
3a391a39 | 2065 | if (device->handler) |
2c22e652 | 2066 | goto ok; |
3a391a39 | 2067 | |
202317a5 RW |
2068 | if (!device->flags.initialized) { |
2069 | acpi_bus_update_power(device, NULL); | |
2070 | device->flags.initialized = true; | |
2071 | } | |
2c22e652 | 2072 | device->flags.visited = false; |
ca589f94 | 2073 | ret = acpi_scan_attach_handler(device); |
6931007c | 2074 | if (ret < 0) |
2c22e652 | 2075 | return; |
6931007c RW |
2076 | |
2077 | device->flags.match_driver = true; | |
2c22e652 RW |
2078 | if (!ret) { |
2079 | ret = device_attach(&device->dev); | |
2080 | if (ret < 0) | |
2081 | return; | |
2082 | } | |
202317a5 | 2083 | device->flags.visited = true; |
202317a5 | 2084 | |
2c22e652 RW |
2085 | ok: |
2086 | list_for_each_entry(child, &device->children, node) | |
2087 | acpi_bus_attach(child); | |
1da177e4 | 2088 | } |
1da177e4 | 2089 | |
636458de | 2090 | /** |
b8bd759a | 2091 | * acpi_bus_scan - Add ACPI device node objects in a given namespace scope. |
636458de | 2092 | * @handle: Root of the namespace scope to scan. |
7779688f | 2093 | * |
636458de RW |
2094 | * Scan a given ACPI tree (probably recently hot-plugged) and create and add |
2095 | * found devices. | |
7779688f | 2096 | * |
636458de RW |
2097 | * If no devices were found, -ENODEV is returned, but it does not mean that |
2098 | * there has been a real error. There just have been no suitable ACPI objects | |
2099 | * in the table trunk from which the kernel could create a device and add an | |
2100 | * appropriate driver. | |
3757b948 RW |
2101 | * |
2102 | * Must be called under acpi_scan_lock. | |
7779688f | 2103 | */ |
b8bd759a | 2104 | int acpi_bus_scan(acpi_handle handle) |
3fb02738 | 2105 | { |
b8bd759a | 2106 | void *device = NULL; |
3fb02738 | 2107 | |
b8bd759a RW |
2108 | if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device))) |
2109 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, | |
2110 | acpi_bus_check_add, NULL, NULL, &device); | |
3fb02738 | 2111 | |
2c22e652 RW |
2112 | if (device) { |
2113 | acpi_bus_attach(device); | |
2114 | return 0; | |
05404d8f | 2115 | } |
2c22e652 | 2116 | return -ENODEV; |
3fb02738 | 2117 | } |
2c22e652 | 2118 | EXPORT_SYMBOL(acpi_bus_scan); |
1da177e4 | 2119 | |
3757b948 | 2120 | /** |
2c22e652 RW |
2121 | * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects. |
2122 | * @adev: Root of the ACPI namespace scope to walk. | |
3757b948 RW |
2123 | * |
2124 | * Must be called under acpi_scan_lock. | |
2125 | */ | |
2c22e652 | 2126 | void acpi_bus_trim(struct acpi_device *adev) |
1da177e4 | 2127 | { |
2c22e652 RW |
2128 | struct acpi_scan_handler *handler = adev->handler; |
2129 | struct acpi_device *child; | |
2130 | ||
2131 | list_for_each_entry_reverse(child, &adev->children, node) | |
2132 | acpi_bus_trim(child); | |
2133 | ||
a951c773 | 2134 | adev->flags.match_driver = false; |
2c22e652 RW |
2135 | if (handler) { |
2136 | if (handler->detach) | |
2137 | handler->detach(adev); | |
2138 | ||
2139 | adev->handler = NULL; | |
2140 | } else { | |
2141 | device_release_driver(&adev->dev); | |
2142 | } | |
05404d8f | 2143 | /* |
2c22e652 RW |
2144 | * Most likely, the device is going away, so put it into D3cold before |
2145 | * that. | |
05404d8f | 2146 | */ |
2c22e652 RW |
2147 | acpi_device_set_power(adev, ACPI_STATE_D3_COLD); |
2148 | adev->flags.initialized = false; | |
2149 | adev->flags.visited = false; | |
1da177e4 | 2150 | } |
ceaba663 KA |
2151 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
2152 | ||
e8b945c9 | 2153 | static int acpi_bus_scan_fixed(void) |
1da177e4 | 2154 | { |
4be44fcd | 2155 | int result = 0; |
c4168bff | 2156 | |
1da177e4 LT |
2157 | /* |
2158 | * Enumerate all fixed-feature devices. | |
2159 | */ | |
2c0d4fe0 RW |
2160 | if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) { |
2161 | struct acpi_device *device = NULL; | |
2162 | ||
5c478f49 | 2163 | result = acpi_add_single_object(&device, NULL, |
c4168bff | 2164 | ACPI_BUS_TYPE_POWER_BUTTON, |
2c0d4fe0 RW |
2165 | ACPI_STA_DEFAULT); |
2166 | if (result) | |
2167 | return result; | |
2168 | ||
88346167 | 2169 | device->flags.match_driver = true; |
2c0d4fe0 RW |
2170 | result = device_attach(&device->dev); |
2171 | if (result < 0) | |
2172 | return result; | |
2173 | ||
c10d7a13 | 2174 | device_init_wakeup(&device->dev, true); |
3fb02738 | 2175 | } |
1da177e4 | 2176 | |
2c0d4fe0 RW |
2177 | if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) { |
2178 | struct acpi_device *device = NULL; | |
2179 | ||
5c478f49 | 2180 | result = acpi_add_single_object(&device, NULL, |
c4168bff | 2181 | ACPI_BUS_TYPE_SLEEP_BUTTON, |
2c0d4fe0 RW |
2182 | ACPI_STA_DEFAULT); |
2183 | if (result) | |
2184 | return result; | |
2185 | ||
88346167 | 2186 | device->flags.match_driver = true; |
2c0d4fe0 | 2187 | result = device_attach(&device->dev); |
3fb02738 | 2188 | } |
1da177e4 | 2189 | |
2c0d4fe0 | 2190 | return result < 0 ? result : 0; |
1da177e4 LT |
2191 | } |
2192 | ||
e747f274 | 2193 | int __init acpi_scan_init(void) |
1da177e4 LT |
2194 | { |
2195 | int result; | |
1da177e4 | 2196 | |
5b327265 PM |
2197 | result = bus_register(&acpi_bus_type); |
2198 | if (result) { | |
2199 | /* We don't want to quit even if we failed to add suspend/resume */ | |
2200 | printk(KERN_ERR PREFIX "Could not register bus type\n"); | |
2201 | } | |
2202 | ||
92ef2a25 | 2203 | acpi_pci_root_init(); |
4daeaf68 | 2204 | acpi_pci_link_init(); |
ac212b69 | 2205 | acpi_processor_init(); |
141a297b | 2206 | acpi_platform_init(); |
f58b082a | 2207 | acpi_lpss_init(); |
2fa97feb | 2208 | acpi_cmos_rtc_init(); |
737f1a9f | 2209 | acpi_container_init(); |
0a347644 | 2210 | acpi_memory_hotplug_init(); |
94add0f8 | 2211 | acpi_dock_init(); |
97d9a9e9 | 2212 | |
3757b948 | 2213 | mutex_lock(&acpi_scan_lock); |
1da177e4 LT |
2214 | /* |
2215 | * Enumerate devices in the ACPI namespace. | |
2216 | */ | |
0cd6ac52 RW |
2217 | result = acpi_bus_scan(ACPI_ROOT_OBJECT); |
2218 | if (result) | |
3757b948 | 2219 | goto out; |
1da177e4 | 2220 | |
0cd6ac52 | 2221 | result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root); |
1da177e4 | 2222 | if (result) |
3757b948 | 2223 | goto out; |
1da177e4 | 2224 | |
b8bd759a RW |
2225 | result = acpi_bus_scan_fixed(); |
2226 | if (result) { | |
202317a5 RW |
2227 | acpi_detach_data(acpi_root->handle, acpi_scan_drop_device); |
2228 | acpi_device_del(acpi_root); | |
2229 | put_device(&acpi_root->dev); | |
3757b948 | 2230 | goto out; |
b8bd759a | 2231 | } |
1da177e4 | 2232 | |
b8bd759a | 2233 | acpi_update_all_gpes(); |
3757b948 RW |
2234 | |
2235 | out: | |
2236 | mutex_unlock(&acpi_scan_lock); | |
2237 | return result; | |
1da177e4 | 2238 | } |