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 <acpi/acpi_drivers.h>
17 #include <asm/pgtable.h>
21 #define _COMPONENT ACPI_BUS_COMPONENT
22 ACPI_MODULE_NAME("scan");
23 extern struct acpi_device
*acpi_root
;
25 #define ACPI_BUS_CLASS "system_bus"
26 #define ACPI_BUS_HID "LNXSYBUS"
27 #define ACPI_BUS_DEVICE_NAME "System Bus"
29 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
31 #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
34 * If set, devices will be hot-removed even if they cannot be put offline
35 * gracefully (from the kernel's standpoint).
37 bool acpi_force_hot_remove
;
39 static const char *dummy_hid
= "device";
41 static LIST_HEAD(acpi_bus_id_list
);
42 static DEFINE_MUTEX(acpi_scan_lock
);
43 static LIST_HEAD(acpi_scan_handlers_list
);
44 DEFINE_MUTEX(acpi_device_lock
);
45 LIST_HEAD(acpi_wakeup_device_list
);
47 struct acpi_device_bus_id
{
49 unsigned int instance_no
;
50 struct list_head node
;
53 void acpi_scan_lock_acquire(void)
55 mutex_lock(&acpi_scan_lock
);
57 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire
);
59 void acpi_scan_lock_release(void)
61 mutex_unlock(&acpi_scan_lock
);
63 EXPORT_SYMBOL_GPL(acpi_scan_lock_release
);
65 int acpi_scan_add_handler(struct acpi_scan_handler
*handler
)
67 if (!handler
|| !handler
->attach
)
70 list_add_tail(&handler
->list_node
, &acpi_scan_handlers_list
);
74 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler
*handler
,
75 const char *hotplug_profile_name
)
79 error
= acpi_scan_add_handler(handler
);
83 acpi_sysfs_add_hotplug_profile(&handler
->hotplug
, hotplug_profile_name
);
88 * Creates hid/cid(s) string needed for modalias and uevent
89 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
90 * char *modalias: "acpi:IBM0001:ACPI0001"
92 static int create_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
97 struct acpi_hardware_id
*id
;
99 if (list_empty(&acpi_dev
->pnp
.ids
))
102 len
= snprintf(modalias
, size
, "acpi:");
105 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
) {
106 count
= snprintf(&modalias
[len
], size
, "%s:", id
->id
);
107 if (count
< 0 || count
>= size
)
113 modalias
[len
] = '\0';
118 acpi_device_modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
119 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
122 /* Device has no HID and no CID or string is >1024 */
123 len
= create_modalias(acpi_dev
, buf
, 1024);
129 static DEVICE_ATTR(modalias
, 0444, acpi_device_modalias_show
, NULL
);
131 static acpi_status
acpi_bus_offline(acpi_handle handle
, u32 lvl
, void *data
,
134 struct acpi_device
*device
= NULL
;
135 struct acpi_device_physical_node
*pn
;
136 bool second_pass
= (bool)data
;
137 acpi_status status
= AE_OK
;
139 if (acpi_bus_get_device(handle
, &device
))
142 if (device
->handler
&& !device
->handler
->hotplug
.enabled
) {
143 *ret_p
= &device
->dev
;
147 mutex_lock(&device
->physical_node_lock
);
149 list_for_each_entry(pn
, &device
->physical_node_list
, node
) {
153 /* Skip devices offlined by the first pass. */
157 pn
->put_online
= false;
159 ret
= device_offline(pn
->dev
);
160 if (acpi_force_hot_remove
)
164 pn
->put_online
= !ret
;
174 mutex_unlock(&device
->physical_node_lock
);
179 static acpi_status
acpi_bus_online(acpi_handle handle
, u32 lvl
, void *data
,
182 struct acpi_device
*device
= NULL
;
183 struct acpi_device_physical_node
*pn
;
185 if (acpi_bus_get_device(handle
, &device
))
188 mutex_lock(&device
->physical_node_lock
);
190 list_for_each_entry(pn
, &device
->physical_node_list
, node
)
191 if (pn
->put_online
) {
192 device_online(pn
->dev
);
193 pn
->put_online
= false;
196 mutex_unlock(&device
->physical_node_lock
);
201 static int acpi_scan_hot_remove(struct acpi_device
*device
)
203 acpi_handle handle
= device
->handle
;
204 struct device
*errdev
;
206 unsigned long long sta
;
209 * Carry out two passes here and ignore errors in the first pass,
210 * because if the devices in question are memory blocks and
211 * CONFIG_MEMCG is set, one of the blocks may hold data structures
212 * that the other blocks depend on, but it is not known in advance which
215 * If the first pass is successful, the second one isn't needed, though.
218 status
= acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
219 NULL
, acpi_bus_offline
, (void *)false,
221 if (status
== AE_SUPPORT
) {
222 dev_warn(errdev
, "Offline disabled.\n");
223 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
224 acpi_bus_online
, NULL
, NULL
, NULL
);
227 acpi_bus_offline(handle
, 0, (void *)false, (void **)&errdev
);
230 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
231 NULL
, acpi_bus_offline
, (void *)true,
233 if (!errdev
|| acpi_force_hot_remove
)
234 acpi_bus_offline(handle
, 0, (void *)true,
237 if (errdev
&& !acpi_force_hot_remove
) {
238 dev_warn(errdev
, "Offline failed.\n");
239 acpi_bus_online(handle
, 0, NULL
, NULL
);
240 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
,
241 ACPI_UINT32_MAX
, acpi_bus_online
,
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
248 "Hot-removing device %s...\n", dev_name(&device
->dev
)));
250 acpi_bus_trim(device
);
252 acpi_evaluate_lck(handle
, 0);
256 status
= acpi_evaluate_ej0(handle
);
257 if (status
== AE_NOT_FOUND
)
259 else if (ACPI_FAILURE(status
))
263 * Verify if eject was indeed successful. If not, log an error
264 * message. No need to call _OST since _EJ0 call was made OK.
266 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
267 if (ACPI_FAILURE(status
)) {
268 acpi_handle_warn(handle
,
269 "Status check after eject failed (0x%x)\n", status
);
270 } else if (sta
& ACPI_STA_DEVICE_ENABLED
) {
271 acpi_handle_warn(handle
,
272 "Eject incomplete - status 0x%llx\n", sta
);
278 static int acpi_scan_device_not_present(struct acpi_device
*adev
)
280 if (!acpi_device_enumerated(adev
)) {
281 dev_warn(&adev
->dev
, "Still not present\n");
288 static int acpi_scan_device_check(struct acpi_device
*adev
)
292 acpi_bus_get_status(adev
);
293 if (adev
->status
.present
|| adev
->status
.functional
) {
295 * This function is only called for device objects for which
296 * matching scan handlers exist. The only situation in which
297 * the scan handler is not attached to this device object yet
298 * is when the device has just appeared (either it wasn't
299 * present at all before or it was removed and then added
303 dev_warn(&adev
->dev
, "Already enumerated\n");
306 error
= acpi_bus_scan(adev
->handle
);
308 dev_warn(&adev
->dev
, "Namespace scan failure\n");
311 if (!adev
->handler
) {
312 dev_warn(&adev
->dev
, "Enumeration failure\n");
316 error
= acpi_scan_device_not_present(adev
);
321 static int acpi_scan_bus_check(struct acpi_device
*adev
)
323 struct acpi_scan_handler
*handler
= adev
->handler
;
324 struct acpi_device
*child
;
327 acpi_bus_get_status(adev
);
328 if (!(adev
->status
.present
|| adev
->status
.functional
)) {
329 acpi_scan_device_not_present(adev
);
332 if (handler
&& handler
->hotplug
.scan_dependent
)
333 return handler
->hotplug
.scan_dependent(adev
);
335 error
= acpi_bus_scan(adev
->handle
);
337 dev_warn(&adev
->dev
, "Namespace scan failure\n");
340 list_for_each_entry(child
, &adev
->children
, node
) {
341 error
= acpi_scan_bus_check(child
);
348 static void acpi_device_hotplug(void *data
, u32 src
)
350 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
351 struct acpi_device
*adev
= data
;
354 lock_device_hotplug();
355 mutex_lock(&acpi_scan_lock
);
358 * The device object's ACPI handle cannot become invalid as long as we
359 * are holding acpi_scan_lock, but it may have become invalid before
360 * that lock was acquired.
362 if (adev
->handle
== INVALID_ACPI_HANDLE
)
366 case ACPI_NOTIFY_BUS_CHECK
:
367 error
= acpi_scan_bus_check(adev
);
369 case ACPI_NOTIFY_DEVICE_CHECK
:
370 error
= acpi_scan_device_check(adev
);
372 case ACPI_NOTIFY_EJECT_REQUEST
:
373 case ACPI_OST_EC_OSPM_EJECT
:
374 error
= acpi_scan_hot_remove(adev
);
381 ost_code
= ACPI_OST_SC_SUCCESS
;
384 acpi_evaluate_hotplug_ost(adev
->handle
, src
, ost_code
, NULL
);
385 put_device(&adev
->dev
);
386 mutex_unlock(&acpi_scan_lock
);
387 unlock_device_hotplug();
390 static void acpi_hotplug_notify_cb(acpi_handle handle
, u32 type
, void *data
)
392 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
393 struct acpi_scan_handler
*handler
= data
;
394 struct acpi_device
*adev
;
397 if (acpi_bus_get_device(handle
, &adev
))
401 case ACPI_NOTIFY_BUS_CHECK
:
402 acpi_handle_debug(handle
, "ACPI_NOTIFY_BUS_CHECK event\n");
404 case ACPI_NOTIFY_DEVICE_CHECK
:
405 acpi_handle_debug(handle
, "ACPI_NOTIFY_DEVICE_CHECK event\n");
407 case ACPI_NOTIFY_EJECT_REQUEST
:
408 acpi_handle_debug(handle
, "ACPI_NOTIFY_EJECT_REQUEST event\n");
409 if (!handler
->hotplug
.enabled
) {
410 acpi_handle_err(handle
, "Eject disabled\n");
411 ost_code
= ACPI_OST_SC_EJECT_NOT_SUPPORTED
;
414 acpi_evaluate_hotplug_ost(handle
, ACPI_NOTIFY_EJECT_REQUEST
,
415 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
418 /* non-hotplug event; possibly handled by other handler */
421 get_device(&adev
->dev
);
422 status
= acpi_hotplug_execute(acpi_device_hotplug
, adev
, type
);
423 if (ACPI_SUCCESS(status
))
426 put_device(&adev
->dev
);
429 acpi_evaluate_hotplug_ost(handle
, type
, ost_code
, NULL
);
432 static ssize_t
real_power_state_show(struct device
*dev
,
433 struct device_attribute
*attr
, char *buf
)
435 struct acpi_device
*adev
= to_acpi_device(dev
);
439 ret
= acpi_device_get_power(adev
, &state
);
443 return sprintf(buf
, "%s\n", acpi_power_state_string(state
));
446 static DEVICE_ATTR(real_power_state
, 0444, real_power_state_show
, NULL
);
448 static ssize_t
power_state_show(struct device
*dev
,
449 struct device_attribute
*attr
, char *buf
)
451 struct acpi_device
*adev
= to_acpi_device(dev
);
453 return sprintf(buf
, "%s\n", acpi_power_state_string(adev
->power
.state
));
456 static DEVICE_ATTR(power_state
, 0444, power_state_show
, NULL
);
459 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
460 const char *buf
, size_t count
)
462 struct acpi_device
*acpi_device
= to_acpi_device(d
);
463 acpi_object_type not_used
;
466 if (!count
|| buf
[0] != '1')
469 if ((!acpi_device
->handler
|| !acpi_device
->handler
->hotplug
.enabled
)
470 && !acpi_device
->driver
)
473 status
= acpi_get_type(acpi_device
->handle
, ¬_used
);
474 if (ACPI_FAILURE(status
) || !acpi_device
->flags
.ejectable
)
477 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
478 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
479 get_device(&acpi_device
->dev
);
480 status
= acpi_hotplug_execute(acpi_device_hotplug
, acpi_device
,
481 ACPI_OST_EC_OSPM_EJECT
);
482 if (ACPI_SUCCESS(status
))
485 put_device(&acpi_device
->dev
);
486 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
487 ACPI_OST_SC_NON_SPECIFIC_FAILURE
, NULL
);
488 return status
== AE_NO_MEMORY
? -ENOMEM
: -EAGAIN
;
491 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
494 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
495 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
497 return sprintf(buf
, "%s\n", acpi_device_hid(acpi_dev
));
499 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
501 static ssize_t
acpi_device_uid_show(struct device
*dev
,
502 struct device_attribute
*attr
, char *buf
)
504 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
506 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.unique_id
);
508 static DEVICE_ATTR(uid
, 0444, acpi_device_uid_show
, NULL
);
510 static ssize_t
acpi_device_adr_show(struct device
*dev
,
511 struct device_attribute
*attr
, char *buf
)
513 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
515 return sprintf(buf
, "0x%08x\n",
516 (unsigned int)(acpi_dev
->pnp
.bus_address
));
518 static DEVICE_ATTR(adr
, 0444, acpi_device_adr_show
, NULL
);
521 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
522 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
523 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
526 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
530 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
535 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
537 /* sysfs file that shows description text from the ACPI _STR method */
538 static ssize_t
description_show(struct device
*dev
,
539 struct device_attribute
*attr
,
541 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
544 if (acpi_dev
->pnp
.str_obj
== NULL
)
548 * The _STR object contains a Unicode identifier for a device.
549 * We need to convert to utf-8 so it can be displayed.
551 result
= utf16s_to_utf8s(
552 (wchar_t *)acpi_dev
->pnp
.str_obj
->buffer
.pointer
,
553 acpi_dev
->pnp
.str_obj
->buffer
.length
,
554 UTF16_LITTLE_ENDIAN
, buf
,
557 buf
[result
++] = '\n';
561 static DEVICE_ATTR(description
, 0444, description_show
, NULL
);
564 acpi_device_sun_show(struct device
*dev
, struct device_attribute
*attr
,
566 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
568 return sprintf(buf
, "%lu\n", acpi_dev
->pnp
.sun
);
570 static DEVICE_ATTR(sun
, 0444, acpi_device_sun_show
, NULL
);
572 static int acpi_device_setup_files(struct acpi_device
*dev
)
574 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
576 unsigned long long sun
;
580 * Devices gotten from FADT don't have a "path" attribute
583 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
588 if (!list_empty(&dev
->pnp
.ids
)) {
589 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
593 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
599 * If device has _STR, 'description' file is created
601 if (acpi_has_method(dev
->handle
, "_STR")) {
602 status
= acpi_evaluate_object(dev
->handle
, "_STR",
604 if (ACPI_FAILURE(status
))
605 buffer
.pointer
= NULL
;
606 dev
->pnp
.str_obj
= buffer
.pointer
;
607 result
= device_create_file(&dev
->dev
, &dev_attr_description
);
612 if (dev
->pnp
.type
.bus_address
)
613 result
= device_create_file(&dev
->dev
, &dev_attr_adr
);
614 if (dev
->pnp
.unique_id
)
615 result
= device_create_file(&dev
->dev
, &dev_attr_uid
);
617 status
= acpi_evaluate_integer(dev
->handle
, "_SUN", NULL
, &sun
);
618 if (ACPI_SUCCESS(status
)) {
619 dev
->pnp
.sun
= (unsigned long)sun
;
620 result
= device_create_file(&dev
->dev
, &dev_attr_sun
);
624 dev
->pnp
.sun
= (unsigned long)-1;
628 * If device has _EJ0, 'eject' file is created that is used to trigger
629 * hot-removal function from userland.
631 if (acpi_has_method(dev
->handle
, "_EJ0")) {
632 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
637 if (dev
->flags
.power_manageable
) {
638 result
= device_create_file(&dev
->dev
, &dev_attr_power_state
);
642 if (dev
->power
.flags
.power_resources
)
643 result
= device_create_file(&dev
->dev
,
644 &dev_attr_real_power_state
);
651 static void acpi_device_remove_files(struct acpi_device
*dev
)
653 if (dev
->flags
.power_manageable
) {
654 device_remove_file(&dev
->dev
, &dev_attr_power_state
);
655 if (dev
->power
.flags
.power_resources
)
656 device_remove_file(&dev
->dev
,
657 &dev_attr_real_power_state
);
661 * If device has _STR, remove 'description' file
663 if (acpi_has_method(dev
->handle
, "_STR")) {
664 kfree(dev
->pnp
.str_obj
);
665 device_remove_file(&dev
->dev
, &dev_attr_description
);
668 * If device has _EJ0, remove 'eject' file.
670 if (acpi_has_method(dev
->handle
, "_EJ0"))
671 device_remove_file(&dev
->dev
, &dev_attr_eject
);
673 if (acpi_has_method(dev
->handle
, "_SUN"))
674 device_remove_file(&dev
->dev
, &dev_attr_sun
);
676 if (dev
->pnp
.unique_id
)
677 device_remove_file(&dev
->dev
, &dev_attr_uid
);
678 if (dev
->pnp
.type
.bus_address
)
679 device_remove_file(&dev
->dev
, &dev_attr_adr
);
680 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
681 device_remove_file(&dev
->dev
, &dev_attr_hid
);
683 device_remove_file(&dev
->dev
, &dev_attr_path
);
685 /* --------------------------------------------------------------------------
687 -------------------------------------------------------------------------- */
689 static const struct acpi_device_id
*__acpi_match_device(
690 struct acpi_device
*device
, const struct acpi_device_id
*ids
)
692 const struct acpi_device_id
*id
;
693 struct acpi_hardware_id
*hwid
;
696 * If the device is not present, it is unnecessary to load device
699 if (!device
->status
.present
)
702 for (id
= ids
; id
->id
[0]; id
++)
703 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
)
704 if (!strcmp((char *) id
->id
, hwid
->id
))
711 * acpi_match_device - Match a struct device against a given list of ACPI IDs
712 * @ids: Array of struct acpi_device_id object to match against.
713 * @dev: The device structure to match.
715 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
716 * object for that handle and use that object to match against a given list of
719 * Return a pointer to the first matching ID on success or %NULL on failure.
721 const struct acpi_device_id
*acpi_match_device(const struct acpi_device_id
*ids
,
722 const struct device
*dev
)
724 struct acpi_device
*adev
;
725 acpi_handle handle
= ACPI_HANDLE(dev
);
727 if (!ids
|| !handle
|| acpi_bus_get_device(handle
, &adev
))
730 return __acpi_match_device(adev
, ids
);
732 EXPORT_SYMBOL_GPL(acpi_match_device
);
734 int acpi_match_device_ids(struct acpi_device
*device
,
735 const struct acpi_device_id
*ids
)
737 return __acpi_match_device(device
, ids
) ? 0 : -ENOENT
;
739 EXPORT_SYMBOL(acpi_match_device_ids
);
741 static void acpi_free_power_resources_lists(struct acpi_device
*device
)
745 if (device
->wakeup
.flags
.valid
)
746 acpi_power_resources_list_free(&device
->wakeup
.resources
);
748 if (!device
->flags
.power_manageable
)
751 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++) {
752 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
753 acpi_power_resources_list_free(&ps
->resources
);
757 static void acpi_device_release(struct device
*dev
)
759 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
761 acpi_free_pnp_ids(&acpi_dev
->pnp
);
762 acpi_free_power_resources_lists(acpi_dev
);
766 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
768 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
769 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
771 return acpi_dev
->flags
.match_driver
772 && !acpi_match_device_ids(acpi_dev
, acpi_drv
->ids
);
775 static int acpi_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
777 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
780 if (list_empty(&acpi_dev
->pnp
.ids
))
783 if (add_uevent_var(env
, "MODALIAS="))
785 len
= create_modalias(acpi_dev
, &env
->buf
[env
->buflen
- 1],
786 sizeof(env
->buf
) - env
->buflen
);
787 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
793 static void acpi_device_notify(acpi_handle handle
, u32 event
, void *data
)
795 struct acpi_device
*device
= data
;
797 device
->driver
->ops
.notify(device
, event
);
800 static acpi_status
acpi_device_notify_fixed(void *data
)
802 struct acpi_device
*device
= data
;
804 /* Fixed hardware devices have no handles */
805 acpi_device_notify(NULL
, ACPI_FIXED_HARDWARE_EVENT
, device
);
809 static int acpi_device_install_notify_handler(struct acpi_device
*device
)
813 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
815 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
816 acpi_device_notify_fixed
,
818 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
820 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
821 acpi_device_notify_fixed
,
824 status
= acpi_install_notify_handler(device
->handle
,
829 if (ACPI_FAILURE(status
))
834 static void acpi_device_remove_notify_handler(struct acpi_device
*device
)
836 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
837 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
838 acpi_device_notify_fixed
);
839 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
840 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
841 acpi_device_notify_fixed
);
843 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
847 static int acpi_device_probe(struct device
*dev
)
849 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
850 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
853 if (acpi_dev
->handler
)
856 if (!acpi_drv
->ops
.add
)
859 ret
= acpi_drv
->ops
.add(acpi_dev
);
863 acpi_dev
->driver
= acpi_drv
;
864 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
865 "Driver [%s] successfully bound to device [%s]\n",
866 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
868 if (acpi_drv
->ops
.notify
) {
869 ret
= acpi_device_install_notify_handler(acpi_dev
);
871 if (acpi_drv
->ops
.remove
)
872 acpi_drv
->ops
.remove(acpi_dev
);
874 acpi_dev
->driver
= NULL
;
875 acpi_dev
->driver_data
= NULL
;
880 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found driver [%s] for device [%s]\n",
881 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
886 static int acpi_device_remove(struct device
* dev
)
888 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
889 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
892 if (acpi_drv
->ops
.notify
)
893 acpi_device_remove_notify_handler(acpi_dev
);
894 if (acpi_drv
->ops
.remove
)
895 acpi_drv
->ops
.remove(acpi_dev
);
897 acpi_dev
->driver
= NULL
;
898 acpi_dev
->driver_data
= NULL
;
904 struct bus_type acpi_bus_type
= {
906 .match
= acpi_bus_match
,
907 .probe
= acpi_device_probe
,
908 .remove
= acpi_device_remove
,
909 .uevent
= acpi_device_uevent
,
912 static void acpi_device_del(struct acpi_device
*device
)
914 mutex_lock(&acpi_device_lock
);
916 list_del(&device
->node
);
918 list_del(&device
->wakeup_list
);
919 mutex_unlock(&acpi_device_lock
);
921 acpi_power_add_remove_device(device
, false);
922 acpi_device_remove_files(device
);
924 device
->remove(device
);
926 device_del(&device
->dev
);
929 static LIST_HEAD(acpi_device_del_list
);
930 static DEFINE_MUTEX(acpi_device_del_lock
);
932 static void acpi_device_del_work_fn(struct work_struct
*work_not_used
)
935 struct acpi_device
*adev
;
937 mutex_lock(&acpi_device_del_lock
);
939 if (list_empty(&acpi_device_del_list
)) {
940 mutex_unlock(&acpi_device_del_lock
);
943 adev
= list_first_entry(&acpi_device_del_list
,
944 struct acpi_device
, del_list
);
945 list_del(&adev
->del_list
);
947 mutex_unlock(&acpi_device_del_lock
);
949 acpi_device_del(adev
);
951 * Drop references to all power resources that might have been
952 * used by the device.
954 acpi_power_transition(adev
, ACPI_STATE_D3_COLD
);
955 put_device(&adev
->dev
);
960 * acpi_scan_drop_device - Drop an ACPI device object.
961 * @handle: Handle of an ACPI namespace node, not used.
962 * @context: Address of the ACPI device object to drop.
964 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
965 * namespace node the device object pointed to by @context is attached to.
967 * The unregistration is carried out asynchronously to avoid running
968 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
969 * ensure the correct ordering (the device objects must be unregistered in the
970 * same order in which the corresponding namespace nodes are deleted).
972 static void acpi_scan_drop_device(acpi_handle handle
, void *context
)
974 static DECLARE_WORK(work
, acpi_device_del_work_fn
);
975 struct acpi_device
*adev
= context
;
977 mutex_lock(&acpi_device_del_lock
);
980 * Use the ACPI hotplug workqueue which is ordered, so this work item
981 * won't run after any hotplug work items submitted subsequently. That
982 * prevents attempts to register device objects identical to those being
983 * deleted from happening concurrently (such attempts result from
984 * hotplug events handled via the ACPI hotplug workqueue). It also will
985 * run after all of the work items submitted previosuly, which helps
986 * those work items to ensure that they are not accessing stale device
989 if (list_empty(&acpi_device_del_list
))
990 acpi_queue_hotplug_work(&work
);
992 list_add_tail(&adev
->del_list
, &acpi_device_del_list
);
993 /* Make acpi_ns_validate_handle() return NULL for this handle. */
994 adev
->handle
= INVALID_ACPI_HANDLE
;
996 mutex_unlock(&acpi_device_del_lock
);
999 int acpi_bus_get_device(acpi_handle handle
, struct acpi_device
**device
)
1006 status
= acpi_get_data(handle
, acpi_scan_drop_device
, (void **)device
);
1007 if (ACPI_FAILURE(status
) || !*device
) {
1008 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
1014 EXPORT_SYMBOL(acpi_bus_get_device
);
1016 int acpi_device_add(struct acpi_device
*device
,
1017 void (*release
)(struct device
*))
1020 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
1023 if (device
->handle
) {
1026 status
= acpi_attach_data(device
->handle
, acpi_scan_drop_device
,
1028 if (ACPI_FAILURE(status
)) {
1029 acpi_handle_err(device
->handle
,
1030 "Unable to attach device data\n");
1038 * Link this device to its parent and siblings.
1040 INIT_LIST_HEAD(&device
->children
);
1041 INIT_LIST_HEAD(&device
->node
);
1042 INIT_LIST_HEAD(&device
->wakeup_list
);
1043 INIT_LIST_HEAD(&device
->physical_node_list
);
1044 INIT_LIST_HEAD(&device
->del_list
);
1045 mutex_init(&device
->physical_node_lock
);
1047 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
1049 pr_err(PREFIX
"Memory allocation error\n");
1054 mutex_lock(&acpi_device_lock
);
1056 * Find suitable bus_id and instance number in acpi_bus_id_list
1057 * If failed, create one and link it into acpi_bus_id_list
1059 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
1060 if (!strcmp(acpi_device_bus_id
->bus_id
,
1061 acpi_device_hid(device
))) {
1062 acpi_device_bus_id
->instance_no
++;
1069 acpi_device_bus_id
= new_bus_id
;
1070 strcpy(acpi_device_bus_id
->bus_id
, acpi_device_hid(device
));
1071 acpi_device_bus_id
->instance_no
= 0;
1072 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
1074 dev_set_name(&device
->dev
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
1077 list_add_tail(&device
->node
, &device
->parent
->children
);
1079 if (device
->wakeup
.flags
.valid
)
1080 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
1081 mutex_unlock(&acpi_device_lock
);
1084 device
->dev
.parent
= &device
->parent
->dev
;
1085 device
->dev
.bus
= &acpi_bus_type
;
1086 device
->dev
.release
= release
;
1087 result
= device_add(&device
->dev
);
1089 dev_err(&device
->dev
, "Error registering device\n");
1093 result
= acpi_device_setup_files(device
);
1095 printk(KERN_ERR PREFIX
"Error creating sysfs interface for device %s\n",
1096 dev_name(&device
->dev
));
1101 mutex_lock(&acpi_device_lock
);
1103 list_del(&device
->node
);
1104 list_del(&device
->wakeup_list
);
1105 mutex_unlock(&acpi_device_lock
);
1108 acpi_detach_data(device
->handle
, acpi_scan_drop_device
);
1112 /* --------------------------------------------------------------------------
1114 -------------------------------------------------------------------------- */
1116 * acpi_bus_register_driver - register a driver with the ACPI bus
1117 * @driver: driver being registered
1119 * Registers a driver with the ACPI bus. Searches the namespace for all
1120 * devices that match the driver's criteria and binds. Returns zero for
1121 * success or a negative error status for failure.
1123 int acpi_bus_register_driver(struct acpi_driver
*driver
)
1129 driver
->drv
.name
= driver
->name
;
1130 driver
->drv
.bus
= &acpi_bus_type
;
1131 driver
->drv
.owner
= driver
->owner
;
1133 ret
= driver_register(&driver
->drv
);
1137 EXPORT_SYMBOL(acpi_bus_register_driver
);
1140 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1141 * @driver: driver to unregister
1143 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1144 * devices that match the driver's criteria and unbinds.
1146 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
1148 driver_unregister(&driver
->drv
);
1151 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
1153 /* --------------------------------------------------------------------------
1155 -------------------------------------------------------------------------- */
1156 static struct acpi_device
*acpi_bus_get_parent(acpi_handle handle
)
1158 struct acpi_device
*device
= NULL
;
1162 * Fixed hardware devices do not appear in the namespace and do not
1163 * have handles, but we fabricate acpi_devices for them, so we have
1164 * to deal with them specially.
1170 status
= acpi_get_parent(handle
, &handle
);
1171 if (ACPI_FAILURE(status
))
1172 return status
== AE_NULL_ENTRY
? NULL
: acpi_root
;
1173 } while (acpi_bus_get_device(handle
, &device
));
1178 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
1182 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1183 union acpi_object
*obj
;
1185 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
1186 if (ACPI_FAILURE(status
))
1189 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
1190 if (ACPI_SUCCESS(status
)) {
1191 obj
= buffer
.pointer
;
1192 status
= acpi_get_handle(ACPI_ROOT_OBJECT
, obj
->string
.pointer
,
1194 kfree(buffer
.pointer
);
1198 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
1200 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle
,
1201 struct acpi_device_wakeup
*wakeup
)
1203 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1204 union acpi_object
*package
= NULL
;
1205 union acpi_object
*element
= NULL
;
1212 INIT_LIST_HEAD(&wakeup
->resources
);
1215 status
= acpi_evaluate_object(handle
, "_PRW", NULL
, &buffer
);
1216 if (ACPI_FAILURE(status
)) {
1217 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
1221 package
= (union acpi_object
*)buffer
.pointer
;
1223 if (!package
|| package
->package
.count
< 2)
1226 element
= &(package
->package
.elements
[0]);
1230 if (element
->type
== ACPI_TYPE_PACKAGE
) {
1231 if ((element
->package
.count
< 2) ||
1232 (element
->package
.elements
[0].type
!=
1233 ACPI_TYPE_LOCAL_REFERENCE
)
1234 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
1237 wakeup
->gpe_device
=
1238 element
->package
.elements
[0].reference
.handle
;
1239 wakeup
->gpe_number
=
1240 (u32
) element
->package
.elements
[1].integer
.value
;
1241 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
1242 wakeup
->gpe_device
= NULL
;
1243 wakeup
->gpe_number
= element
->integer
.value
;
1248 element
= &(package
->package
.elements
[1]);
1249 if (element
->type
!= ACPI_TYPE_INTEGER
)
1252 wakeup
->sleep_state
= element
->integer
.value
;
1254 err
= acpi_extract_power_resources(package
, 2, &wakeup
->resources
);
1258 if (!list_empty(&wakeup
->resources
)) {
1261 err
= acpi_power_wakeup_list_init(&wakeup
->resources
,
1264 acpi_handle_warn(handle
, "Retrieving current states "
1265 "of wakeup power resources failed\n");
1266 acpi_power_resources_list_free(&wakeup
->resources
);
1269 if (sleep_state
< wakeup
->sleep_state
) {
1270 acpi_handle_warn(handle
, "Overriding _PRW sleep state "
1271 "(S%d) by S%d from power resources\n",
1272 (int)wakeup
->sleep_state
, sleep_state
);
1273 wakeup
->sleep_state
= sleep_state
;
1276 acpi_setup_gpe_for_wake(handle
, wakeup
->gpe_device
, wakeup
->gpe_number
);
1279 kfree(buffer
.pointer
);
1283 static void acpi_bus_set_run_wake_flags(struct acpi_device
*device
)
1285 struct acpi_device_id button_device_ids
[] = {
1292 acpi_event_status event_status
;
1294 device
->wakeup
.flags
.notifier_present
= 0;
1296 /* Power button, Lid switch always enable wakeup */
1297 if (!acpi_match_device_ids(device
, button_device_ids
)) {
1298 device
->wakeup
.flags
.run_wake
= 1;
1299 if (!acpi_match_device_ids(device
, &button_device_ids
[1])) {
1300 /* Do not use Lid/sleep button for S5 wakeup */
1301 if (device
->wakeup
.sleep_state
== ACPI_STATE_S5
)
1302 device
->wakeup
.sleep_state
= ACPI_STATE_S4
;
1304 device_set_wakeup_capable(&device
->dev
, true);
1308 status
= acpi_get_gpe_status(device
->wakeup
.gpe_device
,
1309 device
->wakeup
.gpe_number
,
1311 if (status
== AE_OK
)
1312 device
->wakeup
.flags
.run_wake
=
1313 !!(event_status
& ACPI_EVENT_FLAG_HANDLE
);
1316 static void acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
1320 /* Presence of _PRW indicates wake capable */
1321 if (!acpi_has_method(device
->handle
, "_PRW"))
1324 err
= acpi_bus_extract_wakeup_device_power_package(device
->handle
,
1327 dev_err(&device
->dev
, "_PRW evaluation error: %d\n", err
);
1331 device
->wakeup
.flags
.valid
= 1;
1332 device
->wakeup
.prepare_count
= 0;
1333 acpi_bus_set_run_wake_flags(device
);
1334 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1335 * system for the ACPI device with the _PRW object.
1336 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1337 * So it is necessary to call _DSW object first. Only when it is not
1338 * present will the _PSW object used.
1340 err
= acpi_device_sleep_wake(device
, 0, 0, 0);
1342 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1343 "error in _DSW or _PSW evaluation\n"));
1346 static void acpi_bus_init_power_state(struct acpi_device
*device
, int state
)
1348 struct acpi_device_power_state
*ps
= &device
->power
.states
[state
];
1349 char pathname
[5] = { '_', 'P', 'R', '0' + state
, '\0' };
1350 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1353 INIT_LIST_HEAD(&ps
->resources
);
1355 /* Evaluate "_PRx" to get referenced power resources */
1356 status
= acpi_evaluate_object(device
->handle
, pathname
, NULL
, &buffer
);
1357 if (ACPI_SUCCESS(status
)) {
1358 union acpi_object
*package
= buffer
.pointer
;
1360 if (buffer
.length
&& package
1361 && package
->type
== ACPI_TYPE_PACKAGE
1362 && package
->package
.count
) {
1363 int err
= acpi_extract_power_resources(package
, 0,
1366 device
->power
.flags
.power_resources
= 1;
1368 ACPI_FREE(buffer
.pointer
);
1371 /* Evaluate "_PSx" to see if we can do explicit sets */
1373 if (acpi_has_method(device
->handle
, pathname
))
1374 ps
->flags
.explicit_set
= 1;
1377 * State is valid if there are means to put the device into it.
1378 * D3hot is only valid if _PR3 present.
1380 if (!list_empty(&ps
->resources
)
1381 || (ps
->flags
.explicit_set
&& state
< ACPI_STATE_D3_HOT
)) {
1382 ps
->flags
.valid
= 1;
1383 ps
->flags
.os_accessible
= 1;
1386 ps
->power
= -1; /* Unknown - driver assigned */
1387 ps
->latency
= -1; /* Unknown - driver assigned */
1390 static void acpi_bus_get_power_flags(struct acpi_device
*device
)
1394 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1395 if (!acpi_has_method(device
->handle
, "_PS0") &&
1396 !acpi_has_method(device
->handle
, "_PR0"))
1399 device
->flags
.power_manageable
= 1;
1402 * Power Management Flags
1404 if (acpi_has_method(device
->handle
, "_PSC"))
1405 device
->power
.flags
.explicit_get
= 1;
1406 if (acpi_has_method(device
->handle
, "_IRC"))
1407 device
->power
.flags
.inrush_current
= 1;
1410 * Enumerate supported power management states
1412 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++)
1413 acpi_bus_init_power_state(device
, i
);
1415 INIT_LIST_HEAD(&device
->power
.states
[ACPI_STATE_D3_COLD
].resources
);
1417 /* Set defaults for D0 and D3 states (always valid) */
1418 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
1419 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
1420 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.valid
= 1;
1421 device
->power
.states
[ACPI_STATE_D3_COLD
].power
= 0;
1423 /* Set D3cold's explicit_set flag if _PS3 exists. */
1424 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
)
1425 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.explicit_set
= 1;
1427 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1428 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
||
1429 device
->power
.flags
.power_resources
)
1430 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.os_accessible
= 1;
1432 if (acpi_bus_init_power(device
)) {
1433 acpi_free_power_resources_lists(device
);
1434 device
->flags
.power_manageable
= 0;
1438 static void acpi_bus_get_flags(struct acpi_device
*device
)
1440 /* Presence of _STA indicates 'dynamic_status' */
1441 if (acpi_has_method(device
->handle
, "_STA"))
1442 device
->flags
.dynamic_status
= 1;
1444 /* Presence of _RMV indicates 'removable' */
1445 if (acpi_has_method(device
->handle
, "_RMV"))
1446 device
->flags
.removable
= 1;
1448 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1449 if (acpi_has_method(device
->handle
, "_EJD") ||
1450 acpi_has_method(device
->handle
, "_EJ0"))
1451 device
->flags
.ejectable
= 1;
1454 static void acpi_device_get_busid(struct acpi_device
*device
)
1456 char bus_id
[5] = { '?', 0 };
1457 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
1463 * The device's Bus ID is simply the object name.
1464 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1466 if (ACPI_IS_ROOT_DEVICE(device
)) {
1467 strcpy(device
->pnp
.bus_id
, "ACPI");
1471 switch (device
->device_type
) {
1472 case ACPI_BUS_TYPE_POWER_BUTTON
:
1473 strcpy(device
->pnp
.bus_id
, "PWRF");
1475 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1476 strcpy(device
->pnp
.bus_id
, "SLPF");
1479 acpi_get_name(device
->handle
, ACPI_SINGLE_NAME
, &buffer
);
1480 /* Clean up trailing underscores (if any) */
1481 for (i
= 3; i
> 1; i
--) {
1482 if (bus_id
[i
] == '_')
1487 strcpy(device
->pnp
.bus_id
, bus_id
);
1493 * acpi_ata_match - see if an acpi object is an ATA device
1495 * If an acpi object has one of the ACPI ATA methods defined,
1496 * then we can safely call it an ATA device.
1498 bool acpi_ata_match(acpi_handle handle
)
1500 return acpi_has_method(handle
, "_GTF") ||
1501 acpi_has_method(handle
, "_GTM") ||
1502 acpi_has_method(handle
, "_STM") ||
1503 acpi_has_method(handle
, "_SDD");
1507 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1509 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1510 * then we can safely call it an ejectable drive bay
1512 bool acpi_bay_match(acpi_handle handle
)
1514 acpi_handle phandle
;
1516 if (!acpi_has_method(handle
, "_EJ0"))
1518 if (acpi_ata_match(handle
))
1520 if (ACPI_FAILURE(acpi_get_parent(handle
, &phandle
)))
1523 return acpi_ata_match(phandle
);
1527 * acpi_dock_match - see if an acpi object has a _DCK method
1529 bool acpi_dock_match(acpi_handle handle
)
1531 return acpi_has_method(handle
, "_DCK");
1534 const char *acpi_device_hid(struct acpi_device
*device
)
1536 struct acpi_hardware_id
*hid
;
1538 if (list_empty(&device
->pnp
.ids
))
1541 hid
= list_first_entry(&device
->pnp
.ids
, struct acpi_hardware_id
, list
);
1544 EXPORT_SYMBOL(acpi_device_hid
);
1546 static void acpi_add_id(struct acpi_device_pnp
*pnp
, const char *dev_id
)
1548 struct acpi_hardware_id
*id
;
1550 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
1554 id
->id
= kstrdup(dev_id
, GFP_KERNEL
);
1560 list_add_tail(&id
->list
, &pnp
->ids
);
1561 pnp
->type
.hardware_id
= 1;
1565 * Old IBM workstations have a DSDT bug wherein the SMBus object
1566 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1567 * prefix. Work around this.
1569 static bool acpi_ibm_smbus_match(acpi_handle handle
)
1571 char node_name
[ACPI_PATH_SEGMENT_LENGTH
];
1572 struct acpi_buffer path
= { sizeof(node_name
), node_name
};
1574 if (!dmi_name_in_vendors("IBM"))
1577 /* Look for SMBS object */
1578 if (ACPI_FAILURE(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &path
)) ||
1579 strcmp("SMBS", path
.pointer
))
1582 /* Does it have the necessary (but misnamed) methods? */
1583 if (acpi_has_method(handle
, "SBI") &&
1584 acpi_has_method(handle
, "SBR") &&
1585 acpi_has_method(handle
, "SBW"))
1591 static void acpi_set_pnp_ids(acpi_handle handle
, struct acpi_device_pnp
*pnp
,
1595 struct acpi_device_info
*info
;
1596 struct acpi_pnp_device_id_list
*cid_list
;
1599 switch (device_type
) {
1600 case ACPI_BUS_TYPE_DEVICE
:
1601 if (handle
== ACPI_ROOT_OBJECT
) {
1602 acpi_add_id(pnp
, ACPI_SYSTEM_HID
);
1606 status
= acpi_get_object_info(handle
, &info
);
1607 if (ACPI_FAILURE(status
)) {
1608 pr_err(PREFIX
"%s: Error reading device info\n",
1613 if (info
->valid
& ACPI_VALID_HID
)
1614 acpi_add_id(pnp
, info
->hardware_id
.string
);
1615 if (info
->valid
& ACPI_VALID_CID
) {
1616 cid_list
= &info
->compatible_id_list
;
1617 for (i
= 0; i
< cid_list
->count
; i
++)
1618 acpi_add_id(pnp
, cid_list
->ids
[i
].string
);
1620 if (info
->valid
& ACPI_VALID_ADR
) {
1621 pnp
->bus_address
= info
->address
;
1622 pnp
->type
.bus_address
= 1;
1624 if (info
->valid
& ACPI_VALID_UID
)
1625 pnp
->unique_id
= kstrdup(info
->unique_id
.string
,
1631 * Some devices don't reliably have _HIDs & _CIDs, so add
1632 * synthetic HIDs to make sure drivers can find them.
1634 if (acpi_is_video_device(handle
))
1635 acpi_add_id(pnp
, ACPI_VIDEO_HID
);
1636 else if (acpi_bay_match(handle
))
1637 acpi_add_id(pnp
, ACPI_BAY_HID
);
1638 else if (acpi_dock_match(handle
))
1639 acpi_add_id(pnp
, ACPI_DOCK_HID
);
1640 else if (acpi_ibm_smbus_match(handle
))
1641 acpi_add_id(pnp
, ACPI_SMBUS_IBM_HID
);
1642 else if (list_empty(&pnp
->ids
) && handle
== ACPI_ROOT_OBJECT
) {
1643 acpi_add_id(pnp
, ACPI_BUS_HID
); /* \_SB, LNXSYBUS */
1644 strcpy(pnp
->device_name
, ACPI_BUS_DEVICE_NAME
);
1645 strcpy(pnp
->device_class
, ACPI_BUS_CLASS
);
1649 case ACPI_BUS_TYPE_POWER
:
1650 acpi_add_id(pnp
, ACPI_POWER_HID
);
1652 case ACPI_BUS_TYPE_PROCESSOR
:
1653 acpi_add_id(pnp
, ACPI_PROCESSOR_OBJECT_HID
);
1655 case ACPI_BUS_TYPE_THERMAL
:
1656 acpi_add_id(pnp
, ACPI_THERMAL_HID
);
1658 case ACPI_BUS_TYPE_POWER_BUTTON
:
1659 acpi_add_id(pnp
, ACPI_BUTTON_HID_POWERF
);
1661 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1662 acpi_add_id(pnp
, ACPI_BUTTON_HID_SLEEPF
);
1667 void acpi_free_pnp_ids(struct acpi_device_pnp
*pnp
)
1669 struct acpi_hardware_id
*id
, *tmp
;
1671 list_for_each_entry_safe(id
, tmp
, &pnp
->ids
, list
) {
1675 kfree(pnp
->unique_id
);
1678 void acpi_init_device_object(struct acpi_device
*device
, acpi_handle handle
,
1679 int type
, unsigned long long sta
)
1681 INIT_LIST_HEAD(&device
->pnp
.ids
);
1682 device
->device_type
= type
;
1683 device
->handle
= handle
;
1684 device
->parent
= acpi_bus_get_parent(handle
);
1685 acpi_set_device_status(device
, sta
);
1686 acpi_device_get_busid(device
);
1687 acpi_set_pnp_ids(handle
, &device
->pnp
, type
);
1688 acpi_bus_get_flags(device
);
1689 device
->flags
.match_driver
= false;
1690 device
->flags
.initialized
= true;
1691 device
->flags
.visited
= false;
1692 device_initialize(&device
->dev
);
1693 dev_set_uevent_suppress(&device
->dev
, true);
1696 void acpi_device_add_finalize(struct acpi_device
*device
)
1698 dev_set_uevent_suppress(&device
->dev
, false);
1699 kobject_uevent(&device
->dev
.kobj
, KOBJ_ADD
);
1702 static int acpi_add_single_object(struct acpi_device
**child
,
1703 acpi_handle handle
, int type
,
1704 unsigned long long sta
)
1707 struct acpi_device
*device
;
1708 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1710 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
1712 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1716 acpi_init_device_object(device
, handle
, type
, sta
);
1717 acpi_bus_get_power_flags(device
);
1718 acpi_bus_get_wakeup_device_flags(device
);
1720 result
= acpi_device_add(device
, acpi_device_release
);
1722 acpi_device_release(&device
->dev
);
1726 acpi_power_add_remove_device(device
, true);
1727 acpi_device_add_finalize(device
);
1728 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1729 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Added %s [%s] parent %s\n",
1730 dev_name(&device
->dev
), (char *) buffer
.pointer
,
1731 device
->parent
? dev_name(&device
->parent
->dev
) : "(null)"));
1732 kfree(buffer
.pointer
);
1737 static int acpi_bus_type_and_status(acpi_handle handle
, int *type
,
1738 unsigned long long *sta
)
1741 acpi_object_type acpi_type
;
1743 status
= acpi_get_type(handle
, &acpi_type
);
1744 if (ACPI_FAILURE(status
))
1747 switch (acpi_type
) {
1748 case ACPI_TYPE_ANY
: /* for ACPI_ROOT_OBJECT */
1749 case ACPI_TYPE_DEVICE
:
1750 *type
= ACPI_BUS_TYPE_DEVICE
;
1751 status
= acpi_bus_get_status_handle(handle
, sta
);
1752 if (ACPI_FAILURE(status
))
1755 case ACPI_TYPE_PROCESSOR
:
1756 *type
= ACPI_BUS_TYPE_PROCESSOR
;
1757 status
= acpi_bus_get_status_handle(handle
, sta
);
1758 if (ACPI_FAILURE(status
))
1761 case ACPI_TYPE_THERMAL
:
1762 *type
= ACPI_BUS_TYPE_THERMAL
;
1763 *sta
= ACPI_STA_DEFAULT
;
1765 case ACPI_TYPE_POWER
:
1766 *type
= ACPI_BUS_TYPE_POWER
;
1767 *sta
= ACPI_STA_DEFAULT
;
1776 bool acpi_device_is_present(struct acpi_device
*adev
)
1778 if (adev
->status
.present
|| adev
->status
.functional
)
1781 adev
->flags
.initialized
= false;
1785 static bool acpi_scan_handler_matching(struct acpi_scan_handler
*handler
,
1787 const struct acpi_device_id
**matchid
)
1789 const struct acpi_device_id
*devid
;
1791 for (devid
= handler
->ids
; devid
->id
[0]; devid
++)
1792 if (!strcmp((char *)devid
->id
, idstr
)) {
1802 static struct acpi_scan_handler
*acpi_scan_match_handler(char *idstr
,
1803 const struct acpi_device_id
**matchid
)
1805 struct acpi_scan_handler
*handler
;
1807 list_for_each_entry(handler
, &acpi_scan_handlers_list
, list_node
)
1808 if (acpi_scan_handler_matching(handler
, idstr
, matchid
))
1814 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile
*hotplug
, bool val
)
1816 if (!!hotplug
->enabled
== !!val
)
1819 mutex_lock(&acpi_scan_lock
);
1821 hotplug
->enabled
= val
;
1823 mutex_unlock(&acpi_scan_lock
);
1826 static void acpi_scan_init_hotplug(acpi_handle handle
, int type
)
1828 struct acpi_device_pnp pnp
= {};
1829 struct acpi_hardware_id
*hwid
;
1830 struct acpi_scan_handler
*handler
;
1832 INIT_LIST_HEAD(&pnp
.ids
);
1833 acpi_set_pnp_ids(handle
, &pnp
, type
);
1835 if (!pnp
.type
.hardware_id
)
1839 * This relies on the fact that acpi_install_notify_handler() will not
1840 * install the same notify handler routine twice for the same handle.
1842 list_for_each_entry(hwid
, &pnp
.ids
, list
) {
1843 handler
= acpi_scan_match_handler(hwid
->id
, NULL
);
1845 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1846 acpi_hotplug_notify_cb
, handler
);
1852 acpi_free_pnp_ids(&pnp
);
1855 static acpi_status
acpi_bus_check_add(acpi_handle handle
, u32 lvl_not_used
,
1856 void *not_used
, void **return_value
)
1858 struct acpi_device
*device
= NULL
;
1860 unsigned long long sta
;
1863 acpi_bus_get_device(handle
, &device
);
1867 result
= acpi_bus_type_and_status(handle
, &type
, &sta
);
1871 if (type
== ACPI_BUS_TYPE_POWER
) {
1872 acpi_add_power_resource(handle
);
1876 acpi_scan_init_hotplug(handle
, type
);
1878 acpi_add_single_object(&device
, handle
, type
, sta
);
1880 return AE_CTRL_DEPTH
;
1884 *return_value
= device
;
1889 static int acpi_scan_attach_handler(struct acpi_device
*device
)
1891 struct acpi_hardware_id
*hwid
;
1894 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
) {
1895 const struct acpi_device_id
*devid
;
1896 struct acpi_scan_handler
*handler
;
1898 handler
= acpi_scan_match_handler(hwid
->id
, &devid
);
1900 ret
= handler
->attach(device
, devid
);
1902 device
->handler
= handler
;
1904 } else if (ret
< 0) {
1912 static void acpi_bus_attach(struct acpi_device
*device
)
1914 struct acpi_device
*child
;
1917 acpi_bus_get_status(device
);
1918 /* Skip devices that are not present. */
1919 if (!acpi_device_is_present(device
)) {
1920 device
->flags
.visited
= false;
1923 if (device
->handler
)
1926 if (!device
->flags
.initialized
) {
1927 acpi_bus_update_power(device
, NULL
);
1928 device
->flags
.initialized
= true;
1930 device
->flags
.visited
= false;
1931 ret
= acpi_scan_attach_handler(device
);
1935 device
->flags
.match_driver
= true;
1937 ret
= device_attach(&device
->dev
);
1941 device
->flags
.visited
= true;
1944 list_for_each_entry(child
, &device
->children
, node
)
1945 acpi_bus_attach(child
);
1949 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1950 * @handle: Root of the namespace scope to scan.
1952 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1955 * If no devices were found, -ENODEV is returned, but it does not mean that
1956 * there has been a real error. There just have been no suitable ACPI objects
1957 * in the table trunk from which the kernel could create a device and add an
1958 * appropriate driver.
1960 * Must be called under acpi_scan_lock.
1962 int acpi_bus_scan(acpi_handle handle
)
1964 void *device
= NULL
;
1966 if (ACPI_SUCCESS(acpi_bus_check_add(handle
, 0, NULL
, &device
)))
1967 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
1968 acpi_bus_check_add
, NULL
, NULL
, &device
);
1971 acpi_bus_attach(device
);
1976 EXPORT_SYMBOL(acpi_bus_scan
);
1979 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
1980 * @adev: Root of the ACPI namespace scope to walk.
1982 * Must be called under acpi_scan_lock.
1984 void acpi_bus_trim(struct acpi_device
*adev
)
1986 struct acpi_scan_handler
*handler
= adev
->handler
;
1987 struct acpi_device
*child
;
1989 list_for_each_entry_reverse(child
, &adev
->children
, node
)
1990 acpi_bus_trim(child
);
1993 if (handler
->detach
)
1994 handler
->detach(adev
);
1996 adev
->handler
= NULL
;
1998 device_release_driver(&adev
->dev
);
2001 * Most likely, the device is going away, so put it into D3cold before
2004 acpi_device_set_power(adev
, ACPI_STATE_D3_COLD
);
2005 adev
->flags
.initialized
= false;
2006 adev
->flags
.visited
= false;
2008 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
2010 static int acpi_bus_scan_fixed(void)
2015 * Enumerate all fixed-feature devices.
2017 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
)) {
2018 struct acpi_device
*device
= NULL
;
2020 result
= acpi_add_single_object(&device
, NULL
,
2021 ACPI_BUS_TYPE_POWER_BUTTON
,
2026 device
->flags
.match_driver
= true;
2027 result
= device_attach(&device
->dev
);
2031 device_init_wakeup(&device
->dev
, true);
2034 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
)) {
2035 struct acpi_device
*device
= NULL
;
2037 result
= acpi_add_single_object(&device
, NULL
,
2038 ACPI_BUS_TYPE_SLEEP_BUTTON
,
2043 device
->flags
.match_driver
= true;
2044 result
= device_attach(&device
->dev
);
2047 return result
< 0 ? result
: 0;
2050 int __init
acpi_scan_init(void)
2054 result
= bus_register(&acpi_bus_type
);
2056 /* We don't want to quit even if we failed to add suspend/resume */
2057 printk(KERN_ERR PREFIX
"Could not register bus type\n");
2060 acpi_pci_root_init();
2061 acpi_pci_link_init();
2062 acpi_processor_init();
2063 acpi_platform_init();
2065 acpi_cmos_rtc_init();
2066 acpi_container_init();
2067 acpi_memory_hotplug_init();
2070 mutex_lock(&acpi_scan_lock
);
2072 * Enumerate devices in the ACPI namespace.
2074 result
= acpi_bus_scan(ACPI_ROOT_OBJECT
);
2078 result
= acpi_bus_get_device(ACPI_ROOT_OBJECT
, &acpi_root
);
2082 result
= acpi_bus_scan_fixed();
2084 acpi_detach_data(acpi_root
->handle
, acpi_scan_drop_device
);
2085 acpi_device_del(acpi_root
);
2086 put_device(&acpi_root
->dev
);
2090 acpi_update_all_gpes();
2093 mutex_unlock(&acpi_scan_lock
);