2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/signal.h>
11 #include <linux/kthread.h>
12 #include <linux/dmi.h>
13 #include <linux/nls.h>
15 #include <asm/pgtable.h>
19 #define _COMPONENT ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 extern struct acpi_device
*acpi_root
;
23 #define ACPI_BUS_CLASS "system_bus"
24 #define ACPI_BUS_HID "LNXSYBUS"
25 #define ACPI_BUS_DEVICE_NAME "System Bus"
27 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29 #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
32 * If set, devices will be hot-removed even if they cannot be put offline
33 * gracefully (from the kernel's standpoint).
35 bool acpi_force_hot_remove
;
37 static const char *dummy_hid
= "device";
39 static LIST_HEAD(acpi_bus_id_list
);
40 static DEFINE_MUTEX(acpi_scan_lock
);
41 static LIST_HEAD(acpi_scan_handlers_list
);
42 DEFINE_MUTEX(acpi_device_lock
);
43 LIST_HEAD(acpi_wakeup_device_list
);
45 struct acpi_device_bus_id
{
47 unsigned int instance_no
;
48 struct list_head node
;
51 void acpi_scan_lock_acquire(void)
53 mutex_lock(&acpi_scan_lock
);
55 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire
);
57 void acpi_scan_lock_release(void)
59 mutex_unlock(&acpi_scan_lock
);
61 EXPORT_SYMBOL_GPL(acpi_scan_lock_release
);
63 int acpi_scan_add_handler(struct acpi_scan_handler
*handler
)
65 if (!handler
|| !handler
->attach
)
68 list_add_tail(&handler
->list_node
, &acpi_scan_handlers_list
);
72 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler
*handler
,
73 const char *hotplug_profile_name
)
77 error
= acpi_scan_add_handler(handler
);
81 acpi_sysfs_add_hotplug_profile(&handler
->hotplug
, hotplug_profile_name
);
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"
90 static int create_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
95 struct acpi_hardware_id
*id
;
97 if (list_empty(&acpi_dev
->pnp
.ids
))
100 len
= snprintf(modalias
, size
, "acpi:");
103 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
) {
104 count
= snprintf(&modalias
[len
], size
, "%s:", id
->id
);
105 if (count
< 0 || count
>= size
)
111 modalias
[len
] = '\0';
116 acpi_device_modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
117 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
120 /* Device has no HID and no CID or string is >1024 */
121 len
= create_modalias(acpi_dev
, buf
, 1024);
127 static DEVICE_ATTR(modalias
, 0444, acpi_device_modalias_show
, NULL
);
129 static acpi_status
acpi_bus_offline(acpi_handle handle
, u32 lvl
, void *data
,
132 struct acpi_device
*device
= NULL
;
133 struct acpi_device_physical_node
*pn
;
134 bool second_pass
= (bool)data
;
135 acpi_status status
= AE_OK
;
137 if (acpi_bus_get_device(handle
, &device
))
140 if (device
->handler
&& !device
->handler
->hotplug
.enabled
) {
141 *ret_p
= &device
->dev
;
145 mutex_lock(&device
->physical_node_lock
);
147 list_for_each_entry(pn
, &device
->physical_node_list
, node
) {
151 /* Skip devices offlined by the first pass. */
155 pn
->put_online
= false;
157 ret
= device_offline(pn
->dev
);
158 if (acpi_force_hot_remove
)
162 pn
->put_online
= !ret
;
172 mutex_unlock(&device
->physical_node_lock
);
177 static acpi_status
acpi_bus_online(acpi_handle handle
, u32 lvl
, void *data
,
180 struct acpi_device
*device
= NULL
;
181 struct acpi_device_physical_node
*pn
;
183 if (acpi_bus_get_device(handle
, &device
))
186 mutex_lock(&device
->physical_node_lock
);
188 list_for_each_entry(pn
, &device
->physical_node_list
, node
)
189 if (pn
->put_online
) {
190 device_online(pn
->dev
);
191 pn
->put_online
= false;
194 mutex_unlock(&device
->physical_node_lock
);
199 static int acpi_scan_hot_remove(struct acpi_device
*device
)
201 acpi_handle handle
= device
->handle
;
202 struct device
*errdev
;
204 unsigned long long sta
;
207 * Carry out two passes here and ignore errors in the first pass,
208 * because if the devices in question are memory blocks and
209 * CONFIG_MEMCG is set, one of the blocks may hold data structures
210 * that the other blocks depend on, but it is not known in advance which
213 * If the first pass is successful, the second one isn't needed, though.
216 status
= acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
217 NULL
, acpi_bus_offline
, (void *)false,
219 if (status
== AE_SUPPORT
) {
220 dev_warn(errdev
, "Offline disabled.\n");
221 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
222 acpi_bus_online
, NULL
, NULL
, NULL
);
225 acpi_bus_offline(handle
, 0, (void *)false, (void **)&errdev
);
228 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
229 NULL
, acpi_bus_offline
, (void *)true,
231 if (!errdev
|| acpi_force_hot_remove
)
232 acpi_bus_offline(handle
, 0, (void *)true,
235 if (errdev
&& !acpi_force_hot_remove
) {
236 dev_warn(errdev
, "Offline failed.\n");
237 acpi_bus_online(handle
, 0, NULL
, NULL
);
238 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
,
239 ACPI_UINT32_MAX
, acpi_bus_online
,
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
246 "Hot-removing device %s...\n", dev_name(&device
->dev
)));
248 acpi_bus_trim(device
);
250 acpi_evaluate_lck(handle
, 0);
254 status
= acpi_evaluate_ej0(handle
);
255 if (status
== AE_NOT_FOUND
)
257 else if (ACPI_FAILURE(status
))
261 * Verify if eject was indeed successful. If not, log an error
262 * message. No need to call _OST since _EJ0 call was made OK.
264 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
265 if (ACPI_FAILURE(status
)) {
266 acpi_handle_warn(handle
,
267 "Status check after eject failed (0x%x)\n", status
);
268 } else if (sta
& ACPI_STA_DEVICE_ENABLED
) {
269 acpi_handle_warn(handle
,
270 "Eject incomplete - status 0x%llx\n", sta
);
276 static int acpi_scan_device_not_present(struct acpi_device
*adev
)
278 if (!acpi_device_enumerated(adev
)) {
279 dev_warn(&adev
->dev
, "Still not present\n");
286 static int acpi_scan_device_check(struct acpi_device
*adev
)
290 acpi_bus_get_status(adev
);
291 if (adev
->status
.present
|| adev
->status
.functional
) {
293 * This function is only called for device objects for which
294 * matching scan handlers exist. The only situation in which
295 * the scan handler is not attached to this device object yet
296 * is when the device has just appeared (either it wasn't
297 * present at all before or it was removed and then added
301 dev_warn(&adev
->dev
, "Already enumerated\n");
304 error
= acpi_bus_scan(adev
->handle
);
306 dev_warn(&adev
->dev
, "Namespace scan failure\n");
309 if (!adev
->handler
) {
310 dev_warn(&adev
->dev
, "Enumeration failure\n");
314 error
= acpi_scan_device_not_present(adev
);
319 static int acpi_scan_bus_check(struct acpi_device
*adev
)
321 struct acpi_scan_handler
*handler
= adev
->handler
;
322 struct acpi_device
*child
;
325 acpi_bus_get_status(adev
);
326 if (!(adev
->status
.present
|| adev
->status
.functional
)) {
327 acpi_scan_device_not_present(adev
);
330 if (handler
&& handler
->hotplug
.scan_dependent
)
331 return handler
->hotplug
.scan_dependent(adev
);
333 error
= acpi_bus_scan(adev
->handle
);
335 dev_warn(&adev
->dev
, "Namespace scan failure\n");
338 list_for_each_entry(child
, &adev
->children
, node
) {
339 error
= acpi_scan_bus_check(child
);
346 static void acpi_device_hotplug(void *data
, u32 src
)
348 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
349 struct acpi_device
*adev
= data
;
352 lock_device_hotplug();
353 mutex_lock(&acpi_scan_lock
);
356 * The device object's ACPI handle cannot become invalid as long as we
357 * are holding acpi_scan_lock, but it may have become invalid before
358 * that lock was acquired.
360 if (adev
->handle
== INVALID_ACPI_HANDLE
)
364 case ACPI_NOTIFY_BUS_CHECK
:
365 error
= acpi_scan_bus_check(adev
);
367 case ACPI_NOTIFY_DEVICE_CHECK
:
368 error
= acpi_scan_device_check(adev
);
370 case ACPI_NOTIFY_EJECT_REQUEST
:
371 case ACPI_OST_EC_OSPM_EJECT
:
372 error
= acpi_scan_hot_remove(adev
);
379 ost_code
= ACPI_OST_SC_SUCCESS
;
382 acpi_evaluate_hotplug_ost(adev
->handle
, src
, ost_code
, NULL
);
383 put_device(&adev
->dev
);
384 mutex_unlock(&acpi_scan_lock
);
385 unlock_device_hotplug();
388 static void acpi_hotplug_notify_cb(acpi_handle handle
, u32 type
, void *data
)
390 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
391 struct acpi_scan_handler
*handler
= data
;
392 struct acpi_device
*adev
;
395 if (acpi_bus_get_device(handle
, &adev
))
399 case ACPI_NOTIFY_BUS_CHECK
:
400 acpi_handle_debug(handle
, "ACPI_NOTIFY_BUS_CHECK event\n");
402 case ACPI_NOTIFY_DEVICE_CHECK
:
403 acpi_handle_debug(handle
, "ACPI_NOTIFY_DEVICE_CHECK event\n");
405 case ACPI_NOTIFY_EJECT_REQUEST
:
406 acpi_handle_debug(handle
, "ACPI_NOTIFY_EJECT_REQUEST event\n");
407 if (!handler
->hotplug
.enabled
) {
408 acpi_handle_err(handle
, "Eject disabled\n");
409 ost_code
= ACPI_OST_SC_EJECT_NOT_SUPPORTED
;
412 acpi_evaluate_hotplug_ost(handle
, ACPI_NOTIFY_EJECT_REQUEST
,
413 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
416 /* non-hotplug event; possibly handled by other handler */
419 get_device(&adev
->dev
);
420 status
= acpi_hotplug_execute(acpi_device_hotplug
, adev
, type
);
421 if (ACPI_SUCCESS(status
))
424 put_device(&adev
->dev
);
427 acpi_evaluate_hotplug_ost(handle
, type
, ost_code
, NULL
);
430 static ssize_t
real_power_state_show(struct device
*dev
,
431 struct device_attribute
*attr
, char *buf
)
433 struct acpi_device
*adev
= to_acpi_device(dev
);
437 ret
= acpi_device_get_power(adev
, &state
);
441 return sprintf(buf
, "%s\n", acpi_power_state_string(state
));
444 static DEVICE_ATTR(real_power_state
, 0444, real_power_state_show
, NULL
);
446 static ssize_t
power_state_show(struct device
*dev
,
447 struct device_attribute
*attr
, char *buf
)
449 struct acpi_device
*adev
= to_acpi_device(dev
);
451 return sprintf(buf
, "%s\n", acpi_power_state_string(adev
->power
.state
));
454 static DEVICE_ATTR(power_state
, 0444, power_state_show
, NULL
);
457 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
458 const char *buf
, size_t count
)
460 struct acpi_device
*acpi_device
= to_acpi_device(d
);
461 acpi_object_type not_used
;
464 if (!count
|| buf
[0] != '1')
467 if ((!acpi_device
->handler
|| !acpi_device
->handler
->hotplug
.enabled
)
468 && !acpi_device
->driver
)
471 status
= acpi_get_type(acpi_device
->handle
, ¬_used
);
472 if (ACPI_FAILURE(status
) || !acpi_device
->flags
.ejectable
)
475 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
476 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
477 get_device(&acpi_device
->dev
);
478 status
= acpi_hotplug_execute(acpi_device_hotplug
, acpi_device
,
479 ACPI_OST_EC_OSPM_EJECT
);
480 if (ACPI_SUCCESS(status
))
483 put_device(&acpi_device
->dev
);
484 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
485 ACPI_OST_SC_NON_SPECIFIC_FAILURE
, NULL
);
486 return status
== AE_NO_MEMORY
? -ENOMEM
: -EAGAIN
;
489 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
492 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
493 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
495 return sprintf(buf
, "%s\n", acpi_device_hid(acpi_dev
));
497 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
499 static ssize_t
acpi_device_uid_show(struct device
*dev
,
500 struct device_attribute
*attr
, char *buf
)
502 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
504 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.unique_id
);
506 static DEVICE_ATTR(uid
, 0444, acpi_device_uid_show
, NULL
);
508 static ssize_t
acpi_device_adr_show(struct device
*dev
,
509 struct device_attribute
*attr
, char *buf
)
511 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
513 return sprintf(buf
, "0x%08x\n",
514 (unsigned int)(acpi_dev
->pnp
.bus_address
));
516 static DEVICE_ATTR(adr
, 0444, acpi_device_adr_show
, NULL
);
519 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
520 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
521 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
524 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
528 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
533 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
535 /* sysfs file that shows description text from the ACPI _STR method */
536 static ssize_t
description_show(struct device
*dev
,
537 struct device_attribute
*attr
,
539 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
542 if (acpi_dev
->pnp
.str_obj
== NULL
)
546 * The _STR object contains a Unicode identifier for a device.
547 * We need to convert to utf-8 so it can be displayed.
549 result
= utf16s_to_utf8s(
550 (wchar_t *)acpi_dev
->pnp
.str_obj
->buffer
.pointer
,
551 acpi_dev
->pnp
.str_obj
->buffer
.length
,
552 UTF16_LITTLE_ENDIAN
, buf
,
555 buf
[result
++] = '\n';
559 static DEVICE_ATTR(description
, 0444, description_show
, NULL
);
562 acpi_device_sun_show(struct device
*dev
, struct device_attribute
*attr
,
564 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
566 return sprintf(buf
, "%lu\n", acpi_dev
->pnp
.sun
);
568 static DEVICE_ATTR(sun
, 0444, acpi_device_sun_show
, NULL
);
570 static int acpi_device_setup_files(struct acpi_device
*dev
)
572 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
574 unsigned long long sun
;
578 * Devices gotten from FADT don't have a "path" attribute
581 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
586 if (!list_empty(&dev
->pnp
.ids
)) {
587 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
591 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
597 * If device has _STR, 'description' file is created
599 if (acpi_has_method(dev
->handle
, "_STR")) {
600 status
= acpi_evaluate_object(dev
->handle
, "_STR",
602 if (ACPI_FAILURE(status
))
603 buffer
.pointer
= NULL
;
604 dev
->pnp
.str_obj
= buffer
.pointer
;
605 result
= device_create_file(&dev
->dev
, &dev_attr_description
);
610 if (dev
->pnp
.type
.bus_address
)
611 result
= device_create_file(&dev
->dev
, &dev_attr_adr
);
612 if (dev
->pnp
.unique_id
)
613 result
= device_create_file(&dev
->dev
, &dev_attr_uid
);
615 status
= acpi_evaluate_integer(dev
->handle
, "_SUN", NULL
, &sun
);
616 if (ACPI_SUCCESS(status
)) {
617 dev
->pnp
.sun
= (unsigned long)sun
;
618 result
= device_create_file(&dev
->dev
, &dev_attr_sun
);
622 dev
->pnp
.sun
= (unsigned long)-1;
626 * If device has _EJ0, 'eject' file is created that is used to trigger
627 * hot-removal function from userland.
629 if (acpi_has_method(dev
->handle
, "_EJ0")) {
630 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
635 if (dev
->flags
.power_manageable
) {
636 result
= device_create_file(&dev
->dev
, &dev_attr_power_state
);
640 if (dev
->power
.flags
.power_resources
)
641 result
= device_create_file(&dev
->dev
,
642 &dev_attr_real_power_state
);
649 static void acpi_device_remove_files(struct acpi_device
*dev
)
651 if (dev
->flags
.power_manageable
) {
652 device_remove_file(&dev
->dev
, &dev_attr_power_state
);
653 if (dev
->power
.flags
.power_resources
)
654 device_remove_file(&dev
->dev
,
655 &dev_attr_real_power_state
);
659 * If device has _STR, remove 'description' file
661 if (acpi_has_method(dev
->handle
, "_STR")) {
662 kfree(dev
->pnp
.str_obj
);
663 device_remove_file(&dev
->dev
, &dev_attr_description
);
666 * If device has _EJ0, remove 'eject' file.
668 if (acpi_has_method(dev
->handle
, "_EJ0"))
669 device_remove_file(&dev
->dev
, &dev_attr_eject
);
671 if (acpi_has_method(dev
->handle
, "_SUN"))
672 device_remove_file(&dev
->dev
, &dev_attr_sun
);
674 if (dev
->pnp
.unique_id
)
675 device_remove_file(&dev
->dev
, &dev_attr_uid
);
676 if (dev
->pnp
.type
.bus_address
)
677 device_remove_file(&dev
->dev
, &dev_attr_adr
);
678 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
679 device_remove_file(&dev
->dev
, &dev_attr_hid
);
681 device_remove_file(&dev
->dev
, &dev_attr_path
);
683 /* --------------------------------------------------------------------------
685 -------------------------------------------------------------------------- */
687 static const struct acpi_device_id
*__acpi_match_device(
688 struct acpi_device
*device
, const struct acpi_device_id
*ids
)
690 const struct acpi_device_id
*id
;
691 struct acpi_hardware_id
*hwid
;
694 * If the device is not present, it is unnecessary to load device
697 if (!device
->status
.present
)
700 for (id
= ids
; id
->id
[0]; id
++)
701 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
)
702 if (!strcmp((char *) id
->id
, hwid
->id
))
709 * acpi_match_device - Match a struct device against a given list of ACPI IDs
710 * @ids: Array of struct acpi_device_id object to match against.
711 * @dev: The device structure to match.
713 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
714 * object for that handle and use that object to match against a given list of
717 * Return a pointer to the first matching ID on success or %NULL on failure.
719 const struct acpi_device_id
*acpi_match_device(const struct acpi_device_id
*ids
,
720 const struct device
*dev
)
722 struct acpi_device
*adev
;
723 acpi_handle handle
= ACPI_HANDLE(dev
);
725 if (!ids
|| !handle
|| acpi_bus_get_device(handle
, &adev
))
728 return __acpi_match_device(adev
, ids
);
730 EXPORT_SYMBOL_GPL(acpi_match_device
);
732 int acpi_match_device_ids(struct acpi_device
*device
,
733 const struct acpi_device_id
*ids
)
735 return __acpi_match_device(device
, ids
) ? 0 : -ENOENT
;
737 EXPORT_SYMBOL(acpi_match_device_ids
);
739 static void acpi_free_power_resources_lists(struct acpi_device
*device
)
743 if (device
->wakeup
.flags
.valid
)
744 acpi_power_resources_list_free(&device
->wakeup
.resources
);
746 if (!device
->flags
.power_manageable
)
749 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++) {
750 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
751 acpi_power_resources_list_free(&ps
->resources
);
755 static void acpi_device_release(struct device
*dev
)
757 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
759 acpi_free_pnp_ids(&acpi_dev
->pnp
);
760 acpi_free_power_resources_lists(acpi_dev
);
764 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
766 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
767 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
769 return acpi_dev
->flags
.match_driver
770 && !acpi_match_device_ids(acpi_dev
, acpi_drv
->ids
);
773 static int acpi_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
775 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
778 if (list_empty(&acpi_dev
->pnp
.ids
))
781 if (add_uevent_var(env
, "MODALIAS="))
783 len
= create_modalias(acpi_dev
, &env
->buf
[env
->buflen
- 1],
784 sizeof(env
->buf
) - env
->buflen
);
785 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
791 static void acpi_device_notify(acpi_handle handle
, u32 event
, void *data
)
793 struct acpi_device
*device
= data
;
795 device
->driver
->ops
.notify(device
, event
);
798 static acpi_status
acpi_device_notify_fixed(void *data
)
800 struct acpi_device
*device
= data
;
802 /* Fixed hardware devices have no handles */
803 acpi_device_notify(NULL
, ACPI_FIXED_HARDWARE_EVENT
, device
);
807 static int acpi_device_install_notify_handler(struct acpi_device
*device
)
811 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
813 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
814 acpi_device_notify_fixed
,
816 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
818 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
819 acpi_device_notify_fixed
,
822 status
= acpi_install_notify_handler(device
->handle
,
827 if (ACPI_FAILURE(status
))
832 static void acpi_device_remove_notify_handler(struct acpi_device
*device
)
834 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
835 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
836 acpi_device_notify_fixed
);
837 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
838 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
839 acpi_device_notify_fixed
);
841 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
845 static int acpi_device_probe(struct device
*dev
)
847 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
848 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
851 if (acpi_dev
->handler
)
854 if (!acpi_drv
->ops
.add
)
857 ret
= acpi_drv
->ops
.add(acpi_dev
);
861 acpi_dev
->driver
= acpi_drv
;
862 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
863 "Driver [%s] successfully bound to device [%s]\n",
864 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
866 if (acpi_drv
->ops
.notify
) {
867 ret
= acpi_device_install_notify_handler(acpi_dev
);
869 if (acpi_drv
->ops
.remove
)
870 acpi_drv
->ops
.remove(acpi_dev
);
872 acpi_dev
->driver
= NULL
;
873 acpi_dev
->driver_data
= NULL
;
878 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found driver [%s] for device [%s]\n",
879 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
884 static int acpi_device_remove(struct device
* dev
)
886 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
887 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
890 if (acpi_drv
->ops
.notify
)
891 acpi_device_remove_notify_handler(acpi_dev
);
892 if (acpi_drv
->ops
.remove
)
893 acpi_drv
->ops
.remove(acpi_dev
);
895 acpi_dev
->driver
= NULL
;
896 acpi_dev
->driver_data
= NULL
;
902 struct bus_type acpi_bus_type
= {
904 .match
= acpi_bus_match
,
905 .probe
= acpi_device_probe
,
906 .remove
= acpi_device_remove
,
907 .uevent
= acpi_device_uevent
,
910 static void acpi_device_del(struct acpi_device
*device
)
912 mutex_lock(&acpi_device_lock
);
914 list_del(&device
->node
);
916 list_del(&device
->wakeup_list
);
917 mutex_unlock(&acpi_device_lock
);
919 acpi_power_add_remove_device(device
, false);
920 acpi_device_remove_files(device
);
922 device
->remove(device
);
924 device_del(&device
->dev
);
927 static LIST_HEAD(acpi_device_del_list
);
928 static DEFINE_MUTEX(acpi_device_del_lock
);
930 static void acpi_device_del_work_fn(struct work_struct
*work_not_used
)
933 struct acpi_device
*adev
;
935 mutex_lock(&acpi_device_del_lock
);
937 if (list_empty(&acpi_device_del_list
)) {
938 mutex_unlock(&acpi_device_del_lock
);
941 adev
= list_first_entry(&acpi_device_del_list
,
942 struct acpi_device
, del_list
);
943 list_del(&adev
->del_list
);
945 mutex_unlock(&acpi_device_del_lock
);
947 acpi_device_del(adev
);
949 * Drop references to all power resources that might have been
950 * used by the device.
952 acpi_power_transition(adev
, ACPI_STATE_D3_COLD
);
953 put_device(&adev
->dev
);
958 * acpi_scan_drop_device - Drop an ACPI device object.
959 * @handle: Handle of an ACPI namespace node, not used.
960 * @context: Address of the ACPI device object to drop.
962 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
963 * namespace node the device object pointed to by @context is attached to.
965 * The unregistration is carried out asynchronously to avoid running
966 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
967 * ensure the correct ordering (the device objects must be unregistered in the
968 * same order in which the corresponding namespace nodes are deleted).
970 static void acpi_scan_drop_device(acpi_handle handle
, void *context
)
972 static DECLARE_WORK(work
, acpi_device_del_work_fn
);
973 struct acpi_device
*adev
= context
;
975 mutex_lock(&acpi_device_del_lock
);
978 * Use the ACPI hotplug workqueue which is ordered, so this work item
979 * won't run after any hotplug work items submitted subsequently. That
980 * prevents attempts to register device objects identical to those being
981 * deleted from happening concurrently (such attempts result from
982 * hotplug events handled via the ACPI hotplug workqueue). It also will
983 * run after all of the work items submitted previosuly, which helps
984 * those work items to ensure that they are not accessing stale device
987 if (list_empty(&acpi_device_del_list
))
988 acpi_queue_hotplug_work(&work
);
990 list_add_tail(&adev
->del_list
, &acpi_device_del_list
);
991 /* Make acpi_ns_validate_handle() return NULL for this handle. */
992 adev
->handle
= INVALID_ACPI_HANDLE
;
994 mutex_unlock(&acpi_device_del_lock
);
997 int acpi_bus_get_device(acpi_handle handle
, struct acpi_device
**device
)
1004 status
= acpi_get_data(handle
, acpi_scan_drop_device
, (void **)device
);
1005 if (ACPI_FAILURE(status
) || !*device
) {
1006 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
1012 EXPORT_SYMBOL(acpi_bus_get_device
);
1014 int acpi_device_add(struct acpi_device
*device
,
1015 void (*release
)(struct device
*))
1018 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
1021 if (device
->handle
) {
1024 status
= acpi_attach_data(device
->handle
, acpi_scan_drop_device
,
1026 if (ACPI_FAILURE(status
)) {
1027 acpi_handle_err(device
->handle
,
1028 "Unable to attach device data\n");
1036 * Link this device to its parent and siblings.
1038 INIT_LIST_HEAD(&device
->children
);
1039 INIT_LIST_HEAD(&device
->node
);
1040 INIT_LIST_HEAD(&device
->wakeup_list
);
1041 INIT_LIST_HEAD(&device
->physical_node_list
);
1042 INIT_LIST_HEAD(&device
->del_list
);
1043 mutex_init(&device
->physical_node_lock
);
1045 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
1047 pr_err(PREFIX
"Memory allocation error\n");
1052 mutex_lock(&acpi_device_lock
);
1054 * Find suitable bus_id and instance number in acpi_bus_id_list
1055 * If failed, create one and link it into acpi_bus_id_list
1057 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
1058 if (!strcmp(acpi_device_bus_id
->bus_id
,
1059 acpi_device_hid(device
))) {
1060 acpi_device_bus_id
->instance_no
++;
1067 acpi_device_bus_id
= new_bus_id
;
1068 strcpy(acpi_device_bus_id
->bus_id
, acpi_device_hid(device
));
1069 acpi_device_bus_id
->instance_no
= 0;
1070 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
1072 dev_set_name(&device
->dev
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
1075 list_add_tail(&device
->node
, &device
->parent
->children
);
1077 if (device
->wakeup
.flags
.valid
)
1078 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
1079 mutex_unlock(&acpi_device_lock
);
1082 device
->dev
.parent
= &device
->parent
->dev
;
1083 device
->dev
.bus
= &acpi_bus_type
;
1084 device
->dev
.release
= release
;
1085 result
= device_add(&device
->dev
);
1087 dev_err(&device
->dev
, "Error registering device\n");
1091 result
= acpi_device_setup_files(device
);
1093 printk(KERN_ERR PREFIX
"Error creating sysfs interface for device %s\n",
1094 dev_name(&device
->dev
));
1099 mutex_lock(&acpi_device_lock
);
1101 list_del(&device
->node
);
1102 list_del(&device
->wakeup_list
);
1103 mutex_unlock(&acpi_device_lock
);
1106 acpi_detach_data(device
->handle
, acpi_scan_drop_device
);
1110 /* --------------------------------------------------------------------------
1112 -------------------------------------------------------------------------- */
1114 * acpi_bus_register_driver - register a driver with the ACPI bus
1115 * @driver: driver being registered
1117 * Registers a driver with the ACPI bus. Searches the namespace for all
1118 * devices that match the driver's criteria and binds. Returns zero for
1119 * success or a negative error status for failure.
1121 int acpi_bus_register_driver(struct acpi_driver
*driver
)
1127 driver
->drv
.name
= driver
->name
;
1128 driver
->drv
.bus
= &acpi_bus_type
;
1129 driver
->drv
.owner
= driver
->owner
;
1131 ret
= driver_register(&driver
->drv
);
1135 EXPORT_SYMBOL(acpi_bus_register_driver
);
1138 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1139 * @driver: driver to unregister
1141 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1142 * devices that match the driver's criteria and unbinds.
1144 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
1146 driver_unregister(&driver
->drv
);
1149 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
1151 /* --------------------------------------------------------------------------
1153 -------------------------------------------------------------------------- */
1154 static struct acpi_device
*acpi_bus_get_parent(acpi_handle handle
)
1156 struct acpi_device
*device
= NULL
;
1160 * Fixed hardware devices do not appear in the namespace and do not
1161 * have handles, but we fabricate acpi_devices for them, so we have
1162 * to deal with them specially.
1168 status
= acpi_get_parent(handle
, &handle
);
1169 if (ACPI_FAILURE(status
))
1170 return status
== AE_NULL_ENTRY
? NULL
: acpi_root
;
1171 } while (acpi_bus_get_device(handle
, &device
));
1176 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
1180 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1181 union acpi_object
*obj
;
1183 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
1184 if (ACPI_FAILURE(status
))
1187 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
1188 if (ACPI_SUCCESS(status
)) {
1189 obj
= buffer
.pointer
;
1190 status
= acpi_get_handle(ACPI_ROOT_OBJECT
, obj
->string
.pointer
,
1192 kfree(buffer
.pointer
);
1196 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
1198 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle
,
1199 struct acpi_device_wakeup
*wakeup
)
1201 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1202 union acpi_object
*package
= NULL
;
1203 union acpi_object
*element
= NULL
;
1210 INIT_LIST_HEAD(&wakeup
->resources
);
1213 status
= acpi_evaluate_object(handle
, "_PRW", NULL
, &buffer
);
1214 if (ACPI_FAILURE(status
)) {
1215 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
1219 package
= (union acpi_object
*)buffer
.pointer
;
1221 if (!package
|| package
->package
.count
< 2)
1224 element
= &(package
->package
.elements
[0]);
1228 if (element
->type
== ACPI_TYPE_PACKAGE
) {
1229 if ((element
->package
.count
< 2) ||
1230 (element
->package
.elements
[0].type
!=
1231 ACPI_TYPE_LOCAL_REFERENCE
)
1232 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
1235 wakeup
->gpe_device
=
1236 element
->package
.elements
[0].reference
.handle
;
1237 wakeup
->gpe_number
=
1238 (u32
) element
->package
.elements
[1].integer
.value
;
1239 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
1240 wakeup
->gpe_device
= NULL
;
1241 wakeup
->gpe_number
= element
->integer
.value
;
1246 element
= &(package
->package
.elements
[1]);
1247 if (element
->type
!= ACPI_TYPE_INTEGER
)
1250 wakeup
->sleep_state
= element
->integer
.value
;
1252 err
= acpi_extract_power_resources(package
, 2, &wakeup
->resources
);
1256 if (!list_empty(&wakeup
->resources
)) {
1259 err
= acpi_power_wakeup_list_init(&wakeup
->resources
,
1262 acpi_handle_warn(handle
, "Retrieving current states "
1263 "of wakeup power resources failed\n");
1264 acpi_power_resources_list_free(&wakeup
->resources
);
1267 if (sleep_state
< wakeup
->sleep_state
) {
1268 acpi_handle_warn(handle
, "Overriding _PRW sleep state "
1269 "(S%d) by S%d from power resources\n",
1270 (int)wakeup
->sleep_state
, sleep_state
);
1271 wakeup
->sleep_state
= sleep_state
;
1274 acpi_setup_gpe_for_wake(handle
, wakeup
->gpe_device
, wakeup
->gpe_number
);
1277 kfree(buffer
.pointer
);
1281 static void acpi_bus_set_run_wake_flags(struct acpi_device
*device
)
1283 struct acpi_device_id button_device_ids
[] = {
1290 acpi_event_status event_status
;
1292 device
->wakeup
.flags
.notifier_present
= 0;
1294 /* Power button, Lid switch always enable wakeup */
1295 if (!acpi_match_device_ids(device
, button_device_ids
)) {
1296 device
->wakeup
.flags
.run_wake
= 1;
1297 if (!acpi_match_device_ids(device
, &button_device_ids
[1])) {
1298 /* Do not use Lid/sleep button for S5 wakeup */
1299 if (device
->wakeup
.sleep_state
== ACPI_STATE_S5
)
1300 device
->wakeup
.sleep_state
= ACPI_STATE_S4
;
1302 device_set_wakeup_capable(&device
->dev
, true);
1306 status
= acpi_get_gpe_status(device
->wakeup
.gpe_device
,
1307 device
->wakeup
.gpe_number
,
1309 if (status
== AE_OK
)
1310 device
->wakeup
.flags
.run_wake
=
1311 !!(event_status
& ACPI_EVENT_FLAG_HANDLE
);
1314 static void acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
1318 /* Presence of _PRW indicates wake capable */
1319 if (!acpi_has_method(device
->handle
, "_PRW"))
1322 err
= acpi_bus_extract_wakeup_device_power_package(device
->handle
,
1325 dev_err(&device
->dev
, "_PRW evaluation error: %d\n", err
);
1329 device
->wakeup
.flags
.valid
= 1;
1330 device
->wakeup
.prepare_count
= 0;
1331 acpi_bus_set_run_wake_flags(device
);
1332 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1333 * system for the ACPI device with the _PRW object.
1334 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1335 * So it is necessary to call _DSW object first. Only when it is not
1336 * present will the _PSW object used.
1338 err
= acpi_device_sleep_wake(device
, 0, 0, 0);
1340 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1341 "error in _DSW or _PSW evaluation\n"));
1344 static void acpi_bus_init_power_state(struct acpi_device
*device
, int state
)
1346 struct acpi_device_power_state
*ps
= &device
->power
.states
[state
];
1347 char pathname
[5] = { '_', 'P', 'R', '0' + state
, '\0' };
1348 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1351 INIT_LIST_HEAD(&ps
->resources
);
1353 /* Evaluate "_PRx" to get referenced power resources */
1354 status
= acpi_evaluate_object(device
->handle
, pathname
, NULL
, &buffer
);
1355 if (ACPI_SUCCESS(status
)) {
1356 union acpi_object
*package
= buffer
.pointer
;
1358 if (buffer
.length
&& package
1359 && package
->type
== ACPI_TYPE_PACKAGE
1360 && package
->package
.count
) {
1361 int err
= acpi_extract_power_resources(package
, 0,
1364 device
->power
.flags
.power_resources
= 1;
1366 ACPI_FREE(buffer
.pointer
);
1369 /* Evaluate "_PSx" to see if we can do explicit sets */
1371 if (acpi_has_method(device
->handle
, pathname
))
1372 ps
->flags
.explicit_set
= 1;
1375 * State is valid if there are means to put the device into it.
1376 * D3hot is only valid if _PR3 present.
1378 if (!list_empty(&ps
->resources
)
1379 || (ps
->flags
.explicit_set
&& state
< ACPI_STATE_D3_HOT
)) {
1380 ps
->flags
.valid
= 1;
1381 ps
->flags
.os_accessible
= 1;
1384 ps
->power
= -1; /* Unknown - driver assigned */
1385 ps
->latency
= -1; /* Unknown - driver assigned */
1388 static void acpi_bus_get_power_flags(struct acpi_device
*device
)
1392 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1393 if (!acpi_has_method(device
->handle
, "_PS0") &&
1394 !acpi_has_method(device
->handle
, "_PR0"))
1397 device
->flags
.power_manageable
= 1;
1400 * Power Management Flags
1402 if (acpi_has_method(device
->handle
, "_PSC"))
1403 device
->power
.flags
.explicit_get
= 1;
1404 if (acpi_has_method(device
->handle
, "_IRC"))
1405 device
->power
.flags
.inrush_current
= 1;
1408 * Enumerate supported power management states
1410 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++)
1411 acpi_bus_init_power_state(device
, i
);
1413 INIT_LIST_HEAD(&device
->power
.states
[ACPI_STATE_D3_COLD
].resources
);
1415 /* Set defaults for D0 and D3 states (always valid) */
1416 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
1417 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
1418 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.valid
= 1;
1419 device
->power
.states
[ACPI_STATE_D3_COLD
].power
= 0;
1421 /* Set D3cold's explicit_set flag if _PS3 exists. */
1422 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
)
1423 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.explicit_set
= 1;
1425 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1426 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
||
1427 device
->power
.flags
.power_resources
)
1428 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.os_accessible
= 1;
1430 if (acpi_bus_init_power(device
)) {
1431 acpi_free_power_resources_lists(device
);
1432 device
->flags
.power_manageable
= 0;
1436 static void acpi_bus_get_flags(struct acpi_device
*device
)
1438 /* Presence of _STA indicates 'dynamic_status' */
1439 if (acpi_has_method(device
->handle
, "_STA"))
1440 device
->flags
.dynamic_status
= 1;
1442 /* Presence of _RMV indicates 'removable' */
1443 if (acpi_has_method(device
->handle
, "_RMV"))
1444 device
->flags
.removable
= 1;
1446 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1447 if (acpi_has_method(device
->handle
, "_EJD") ||
1448 acpi_has_method(device
->handle
, "_EJ0"))
1449 device
->flags
.ejectable
= 1;
1452 static void acpi_device_get_busid(struct acpi_device
*device
)
1454 char bus_id
[5] = { '?', 0 };
1455 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
1461 * The device's Bus ID is simply the object name.
1462 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1464 if (ACPI_IS_ROOT_DEVICE(device
)) {
1465 strcpy(device
->pnp
.bus_id
, "ACPI");
1469 switch (device
->device_type
) {
1470 case ACPI_BUS_TYPE_POWER_BUTTON
:
1471 strcpy(device
->pnp
.bus_id
, "PWRF");
1473 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1474 strcpy(device
->pnp
.bus_id
, "SLPF");
1477 acpi_get_name(device
->handle
, ACPI_SINGLE_NAME
, &buffer
);
1478 /* Clean up trailing underscores (if any) */
1479 for (i
= 3; i
> 1; i
--) {
1480 if (bus_id
[i
] == '_')
1485 strcpy(device
->pnp
.bus_id
, bus_id
);
1491 * acpi_ata_match - see if an acpi object is an ATA device
1493 * If an acpi object has one of the ACPI ATA methods defined,
1494 * then we can safely call it an ATA device.
1496 bool acpi_ata_match(acpi_handle handle
)
1498 return acpi_has_method(handle
, "_GTF") ||
1499 acpi_has_method(handle
, "_GTM") ||
1500 acpi_has_method(handle
, "_STM") ||
1501 acpi_has_method(handle
, "_SDD");
1505 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1507 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1508 * then we can safely call it an ejectable drive bay
1510 bool acpi_bay_match(acpi_handle handle
)
1512 acpi_handle phandle
;
1514 if (!acpi_has_method(handle
, "_EJ0"))
1516 if (acpi_ata_match(handle
))
1518 if (ACPI_FAILURE(acpi_get_parent(handle
, &phandle
)))
1521 return acpi_ata_match(phandle
);
1525 * acpi_dock_match - see if an acpi object has a _DCK method
1527 bool acpi_dock_match(acpi_handle handle
)
1529 return acpi_has_method(handle
, "_DCK");
1532 const char *acpi_device_hid(struct acpi_device
*device
)
1534 struct acpi_hardware_id
*hid
;
1536 if (list_empty(&device
->pnp
.ids
))
1539 hid
= list_first_entry(&device
->pnp
.ids
, struct acpi_hardware_id
, list
);
1542 EXPORT_SYMBOL(acpi_device_hid
);
1544 static void acpi_add_id(struct acpi_device_pnp
*pnp
, const char *dev_id
)
1546 struct acpi_hardware_id
*id
;
1548 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
1552 id
->id
= kstrdup(dev_id
, GFP_KERNEL
);
1558 list_add_tail(&id
->list
, &pnp
->ids
);
1559 pnp
->type
.hardware_id
= 1;
1563 * Old IBM workstations have a DSDT bug wherein the SMBus object
1564 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1565 * prefix. Work around this.
1567 static bool acpi_ibm_smbus_match(acpi_handle handle
)
1569 char node_name
[ACPI_PATH_SEGMENT_LENGTH
];
1570 struct acpi_buffer path
= { sizeof(node_name
), node_name
};
1572 if (!dmi_name_in_vendors("IBM"))
1575 /* Look for SMBS object */
1576 if (ACPI_FAILURE(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &path
)) ||
1577 strcmp("SMBS", path
.pointer
))
1580 /* Does it have the necessary (but misnamed) methods? */
1581 if (acpi_has_method(handle
, "SBI") &&
1582 acpi_has_method(handle
, "SBR") &&
1583 acpi_has_method(handle
, "SBW"))
1589 static void acpi_set_pnp_ids(acpi_handle handle
, struct acpi_device_pnp
*pnp
,
1593 struct acpi_device_info
*info
;
1594 struct acpi_pnp_device_id_list
*cid_list
;
1597 switch (device_type
) {
1598 case ACPI_BUS_TYPE_DEVICE
:
1599 if (handle
== ACPI_ROOT_OBJECT
) {
1600 acpi_add_id(pnp
, ACPI_SYSTEM_HID
);
1604 status
= acpi_get_object_info(handle
, &info
);
1605 if (ACPI_FAILURE(status
)) {
1606 pr_err(PREFIX
"%s: Error reading device info\n",
1611 if (info
->valid
& ACPI_VALID_HID
)
1612 acpi_add_id(pnp
, info
->hardware_id
.string
);
1613 if (info
->valid
& ACPI_VALID_CID
) {
1614 cid_list
= &info
->compatible_id_list
;
1615 for (i
= 0; i
< cid_list
->count
; i
++)
1616 acpi_add_id(pnp
, cid_list
->ids
[i
].string
);
1618 if (info
->valid
& ACPI_VALID_ADR
) {
1619 pnp
->bus_address
= info
->address
;
1620 pnp
->type
.bus_address
= 1;
1622 if (info
->valid
& ACPI_VALID_UID
)
1623 pnp
->unique_id
= kstrdup(info
->unique_id
.string
,
1629 * Some devices don't reliably have _HIDs & _CIDs, so add
1630 * synthetic HIDs to make sure drivers can find them.
1632 if (acpi_is_video_device(handle
))
1633 acpi_add_id(pnp
, ACPI_VIDEO_HID
);
1634 else if (acpi_bay_match(handle
))
1635 acpi_add_id(pnp
, ACPI_BAY_HID
);
1636 else if (acpi_dock_match(handle
))
1637 acpi_add_id(pnp
, ACPI_DOCK_HID
);
1638 else if (acpi_ibm_smbus_match(handle
))
1639 acpi_add_id(pnp
, ACPI_SMBUS_IBM_HID
);
1640 else if (list_empty(&pnp
->ids
) && handle
== ACPI_ROOT_OBJECT
) {
1641 acpi_add_id(pnp
, ACPI_BUS_HID
); /* \_SB, LNXSYBUS */
1642 strcpy(pnp
->device_name
, ACPI_BUS_DEVICE_NAME
);
1643 strcpy(pnp
->device_class
, ACPI_BUS_CLASS
);
1647 case ACPI_BUS_TYPE_POWER
:
1648 acpi_add_id(pnp
, ACPI_POWER_HID
);
1650 case ACPI_BUS_TYPE_PROCESSOR
:
1651 acpi_add_id(pnp
, ACPI_PROCESSOR_OBJECT_HID
);
1653 case ACPI_BUS_TYPE_THERMAL
:
1654 acpi_add_id(pnp
, ACPI_THERMAL_HID
);
1656 case ACPI_BUS_TYPE_POWER_BUTTON
:
1657 acpi_add_id(pnp
, ACPI_BUTTON_HID_POWERF
);
1659 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1660 acpi_add_id(pnp
, ACPI_BUTTON_HID_SLEEPF
);
1665 void acpi_free_pnp_ids(struct acpi_device_pnp
*pnp
)
1667 struct acpi_hardware_id
*id
, *tmp
;
1669 list_for_each_entry_safe(id
, tmp
, &pnp
->ids
, list
) {
1673 kfree(pnp
->unique_id
);
1676 void acpi_init_device_object(struct acpi_device
*device
, acpi_handle handle
,
1677 int type
, unsigned long long sta
)
1679 INIT_LIST_HEAD(&device
->pnp
.ids
);
1680 device
->device_type
= type
;
1681 device
->handle
= handle
;
1682 device
->parent
= acpi_bus_get_parent(handle
);
1683 acpi_set_device_status(device
, sta
);
1684 acpi_device_get_busid(device
);
1685 acpi_set_pnp_ids(handle
, &device
->pnp
, type
);
1686 acpi_bus_get_flags(device
);
1687 device
->flags
.match_driver
= false;
1688 device
->flags
.initialized
= true;
1689 device
->flags
.visited
= false;
1690 device_initialize(&device
->dev
);
1691 dev_set_uevent_suppress(&device
->dev
, true);
1694 void acpi_device_add_finalize(struct acpi_device
*device
)
1696 dev_set_uevent_suppress(&device
->dev
, false);
1697 kobject_uevent(&device
->dev
.kobj
, KOBJ_ADD
);
1700 static int acpi_add_single_object(struct acpi_device
**child
,
1701 acpi_handle handle
, int type
,
1702 unsigned long long sta
)
1705 struct acpi_device
*device
;
1706 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1708 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
1710 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1714 acpi_init_device_object(device
, handle
, type
, sta
);
1715 acpi_bus_get_power_flags(device
);
1716 acpi_bus_get_wakeup_device_flags(device
);
1718 result
= acpi_device_add(device
, acpi_device_release
);
1720 acpi_device_release(&device
->dev
);
1724 acpi_power_add_remove_device(device
, true);
1725 acpi_device_add_finalize(device
);
1726 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1727 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Added %s [%s] parent %s\n",
1728 dev_name(&device
->dev
), (char *) buffer
.pointer
,
1729 device
->parent
? dev_name(&device
->parent
->dev
) : "(null)"));
1730 kfree(buffer
.pointer
);
1735 static int acpi_bus_type_and_status(acpi_handle handle
, int *type
,
1736 unsigned long long *sta
)
1739 acpi_object_type acpi_type
;
1741 status
= acpi_get_type(handle
, &acpi_type
);
1742 if (ACPI_FAILURE(status
))
1745 switch (acpi_type
) {
1746 case ACPI_TYPE_ANY
: /* for ACPI_ROOT_OBJECT */
1747 case ACPI_TYPE_DEVICE
:
1748 *type
= ACPI_BUS_TYPE_DEVICE
;
1749 status
= acpi_bus_get_status_handle(handle
, sta
);
1750 if (ACPI_FAILURE(status
))
1753 case ACPI_TYPE_PROCESSOR
:
1754 *type
= ACPI_BUS_TYPE_PROCESSOR
;
1755 status
= acpi_bus_get_status_handle(handle
, sta
);
1756 if (ACPI_FAILURE(status
))
1759 case ACPI_TYPE_THERMAL
:
1760 *type
= ACPI_BUS_TYPE_THERMAL
;
1761 *sta
= ACPI_STA_DEFAULT
;
1763 case ACPI_TYPE_POWER
:
1764 *type
= ACPI_BUS_TYPE_POWER
;
1765 *sta
= ACPI_STA_DEFAULT
;
1774 bool acpi_device_is_present(struct acpi_device
*adev
)
1776 if (adev
->status
.present
|| adev
->status
.functional
)
1779 adev
->flags
.initialized
= false;
1783 static bool acpi_scan_handler_matching(struct acpi_scan_handler
*handler
,
1785 const struct acpi_device_id
**matchid
)
1787 const struct acpi_device_id
*devid
;
1789 for (devid
= handler
->ids
; devid
->id
[0]; devid
++)
1790 if (!strcmp((char *)devid
->id
, idstr
)) {
1800 static struct acpi_scan_handler
*acpi_scan_match_handler(char *idstr
,
1801 const struct acpi_device_id
**matchid
)
1803 struct acpi_scan_handler
*handler
;
1805 list_for_each_entry(handler
, &acpi_scan_handlers_list
, list_node
)
1806 if (acpi_scan_handler_matching(handler
, idstr
, matchid
))
1812 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile
*hotplug
, bool val
)
1814 if (!!hotplug
->enabled
== !!val
)
1817 mutex_lock(&acpi_scan_lock
);
1819 hotplug
->enabled
= val
;
1821 mutex_unlock(&acpi_scan_lock
);
1824 static void acpi_scan_init_hotplug(acpi_handle handle
, int type
)
1826 struct acpi_device_pnp pnp
= {};
1827 struct acpi_hardware_id
*hwid
;
1828 struct acpi_scan_handler
*handler
;
1830 INIT_LIST_HEAD(&pnp
.ids
);
1831 acpi_set_pnp_ids(handle
, &pnp
, type
);
1833 if (!pnp
.type
.hardware_id
)
1837 * This relies on the fact that acpi_install_notify_handler() will not
1838 * install the same notify handler routine twice for the same handle.
1840 list_for_each_entry(hwid
, &pnp
.ids
, list
) {
1841 handler
= acpi_scan_match_handler(hwid
->id
, NULL
);
1843 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1844 acpi_hotplug_notify_cb
, handler
);
1850 acpi_free_pnp_ids(&pnp
);
1853 static acpi_status
acpi_bus_check_add(acpi_handle handle
, u32 lvl_not_used
,
1854 void *not_used
, void **return_value
)
1856 struct acpi_device
*device
= NULL
;
1858 unsigned long long sta
;
1861 acpi_bus_get_device(handle
, &device
);
1865 result
= acpi_bus_type_and_status(handle
, &type
, &sta
);
1869 if (type
== ACPI_BUS_TYPE_POWER
) {
1870 acpi_add_power_resource(handle
);
1874 acpi_scan_init_hotplug(handle
, type
);
1876 acpi_add_single_object(&device
, handle
, type
, sta
);
1878 return AE_CTRL_DEPTH
;
1882 *return_value
= device
;
1887 static int acpi_scan_attach_handler(struct acpi_device
*device
)
1889 struct acpi_hardware_id
*hwid
;
1892 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
) {
1893 const struct acpi_device_id
*devid
;
1894 struct acpi_scan_handler
*handler
;
1896 handler
= acpi_scan_match_handler(hwid
->id
, &devid
);
1898 ret
= handler
->attach(device
, devid
);
1900 device
->handler
= handler
;
1902 } else if (ret
< 0) {
1910 static void acpi_bus_attach(struct acpi_device
*device
)
1912 struct acpi_device
*child
;
1915 acpi_bus_get_status(device
);
1916 /* Skip devices that are not present. */
1917 if (!acpi_device_is_present(device
)) {
1918 device
->flags
.visited
= false;
1921 if (device
->handler
)
1924 if (!device
->flags
.initialized
) {
1925 acpi_bus_update_power(device
, NULL
);
1926 device
->flags
.initialized
= true;
1928 device
->flags
.visited
= false;
1929 ret
= acpi_scan_attach_handler(device
);
1933 device
->flags
.match_driver
= true;
1935 ret
= device_attach(&device
->dev
);
1939 device
->flags
.visited
= true;
1942 list_for_each_entry(child
, &device
->children
, node
)
1943 acpi_bus_attach(child
);
1947 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1948 * @handle: Root of the namespace scope to scan.
1950 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1953 * If no devices were found, -ENODEV is returned, but it does not mean that
1954 * there has been a real error. There just have been no suitable ACPI objects
1955 * in the table trunk from which the kernel could create a device and add an
1956 * appropriate driver.
1958 * Must be called under acpi_scan_lock.
1960 int acpi_bus_scan(acpi_handle handle
)
1962 void *device
= NULL
;
1964 if (ACPI_SUCCESS(acpi_bus_check_add(handle
, 0, NULL
, &device
)))
1965 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
1966 acpi_bus_check_add
, NULL
, NULL
, &device
);
1969 acpi_bus_attach(device
);
1974 EXPORT_SYMBOL(acpi_bus_scan
);
1977 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
1978 * @adev: Root of the ACPI namespace scope to walk.
1980 * Must be called under acpi_scan_lock.
1982 void acpi_bus_trim(struct acpi_device
*adev
)
1984 struct acpi_scan_handler
*handler
= adev
->handler
;
1985 struct acpi_device
*child
;
1987 list_for_each_entry_reverse(child
, &adev
->children
, node
)
1988 acpi_bus_trim(child
);
1991 if (handler
->detach
)
1992 handler
->detach(adev
);
1994 adev
->handler
= NULL
;
1996 device_release_driver(&adev
->dev
);
1999 * Most likely, the device is going away, so put it into D3cold before
2002 acpi_device_set_power(adev
, ACPI_STATE_D3_COLD
);
2003 adev
->flags
.initialized
= false;
2004 adev
->flags
.visited
= false;
2006 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
2008 static int acpi_bus_scan_fixed(void)
2013 * Enumerate all fixed-feature devices.
2015 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
)) {
2016 struct acpi_device
*device
= NULL
;
2018 result
= acpi_add_single_object(&device
, NULL
,
2019 ACPI_BUS_TYPE_POWER_BUTTON
,
2024 device
->flags
.match_driver
= true;
2025 result
= device_attach(&device
->dev
);
2029 device_init_wakeup(&device
->dev
, true);
2032 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
)) {
2033 struct acpi_device
*device
= NULL
;
2035 result
= acpi_add_single_object(&device
, NULL
,
2036 ACPI_BUS_TYPE_SLEEP_BUTTON
,
2041 device
->flags
.match_driver
= true;
2042 result
= device_attach(&device
->dev
);
2045 return result
< 0 ? result
: 0;
2048 int __init
acpi_scan_init(void)
2052 result
= bus_register(&acpi_bus_type
);
2054 /* We don't want to quit even if we failed to add suspend/resume */
2055 printk(KERN_ERR PREFIX
"Could not register bus type\n");
2058 acpi_pci_root_init();
2059 acpi_pci_link_init();
2060 acpi_processor_init();
2061 acpi_platform_init();
2063 acpi_cmos_rtc_init();
2064 acpi_container_init();
2065 acpi_memory_hotplug_init();
2068 mutex_lock(&acpi_scan_lock
);
2070 * Enumerate devices in the ACPI namespace.
2072 result
= acpi_bus_scan(ACPI_ROOT_OBJECT
);
2076 result
= acpi_bus_get_device(ACPI_ROOT_OBJECT
, &acpi_root
);
2080 result
= acpi_bus_scan_fixed();
2082 acpi_detach_data(acpi_root
->handle
, acpi_scan_drop_device
);
2083 acpi_device_del(acpi_root
);
2084 put_device(&acpi_root
->dev
);
2088 acpi_update_all_gpes();
2091 mutex_unlock(&acpi_scan_lock
);