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>
19 #define _COMPONENT ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 #define STRUCT_TO_INT(s) (*((int*)&s))
22 extern struct acpi_device
*acpi_root
;
24 #define ACPI_BUS_CLASS "system_bus"
25 #define ACPI_BUS_HID "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME "System Bus"
28 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
34 bool acpi_force_hot_remove
;
36 static const char *dummy_hid
= "device";
38 static LIST_HEAD(acpi_bus_id_list
);
39 static DEFINE_MUTEX(acpi_scan_lock
);
40 static LIST_HEAD(acpi_scan_handlers_list
);
41 DEFINE_MUTEX(acpi_device_lock
);
42 LIST_HEAD(acpi_wakeup_device_list
);
44 struct acpi_device_bus_id
{
46 unsigned int instance_no
;
47 struct list_head node
;
50 void acpi_scan_lock_acquire(void)
52 mutex_lock(&acpi_scan_lock
);
54 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire
);
56 void acpi_scan_lock_release(void)
58 mutex_unlock(&acpi_scan_lock
);
60 EXPORT_SYMBOL_GPL(acpi_scan_lock_release
);
62 int acpi_scan_add_handler(struct acpi_scan_handler
*handler
)
64 if (!handler
|| !handler
->attach
)
67 list_add_tail(&handler
->list_node
, &acpi_scan_handlers_list
);
71 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler
*handler
,
72 const char *hotplug_profile_name
)
76 error
= acpi_scan_add_handler(handler
);
80 acpi_sysfs_add_hotplug_profile(&handler
->hotplug
, hotplug_profile_name
);
85 * Creates hid/cid(s) string needed for modalias and uevent
86 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
87 * char *modalias: "acpi:IBM0001:ACPI0001"
89 static int create_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
94 struct acpi_hardware_id
*id
;
96 if (list_empty(&acpi_dev
->pnp
.ids
))
99 len
= snprintf(modalias
, size
, "acpi:");
102 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
) {
103 count
= snprintf(&modalias
[len
], size
, "%s:", id
->id
);
104 if (count
< 0 || count
>= size
)
110 modalias
[len
] = '\0';
115 acpi_device_modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
116 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
119 /* Device has no HID and no CID or string is >1024 */
120 len
= create_modalias(acpi_dev
, buf
, 1024);
126 static DEVICE_ATTR(modalias
, 0444, acpi_device_modalias_show
, NULL
);
128 static acpi_status
acpi_bus_offline_companions(acpi_handle handle
, u32 lvl
,
129 void *data
, void **ret_p
)
131 struct acpi_device
*device
= NULL
;
132 struct acpi_device_physical_node
*pn
;
133 bool second_pass
= (bool)data
;
134 acpi_status status
= AE_OK
;
136 if (acpi_bus_get_device(handle
, &device
))
139 mutex_lock(&device
->physical_node_lock
);
141 list_for_each_entry(pn
, &device
->physical_node_list
, node
) {
145 /* Skip devices offlined by the first pass. */
149 pn
->put_online
= false;
151 ret
= device_offline(pn
->dev
);
152 if (acpi_force_hot_remove
)
156 pn
->put_online
= !ret
;
166 mutex_unlock(&device
->physical_node_lock
);
171 static acpi_status
acpi_bus_online_companions(acpi_handle handle
, u32 lvl
,
172 void *data
, void **ret_p
)
174 struct acpi_device
*device
= NULL
;
175 struct acpi_device_physical_node
*pn
;
177 if (acpi_bus_get_device(handle
, &device
))
180 mutex_lock(&device
->physical_node_lock
);
182 list_for_each_entry(pn
, &device
->physical_node_list
, node
)
183 if (pn
->put_online
) {
184 device_online(pn
->dev
);
185 pn
->put_online
= false;
188 mutex_unlock(&device
->physical_node_lock
);
193 static int acpi_scan_hot_remove(struct acpi_device
*device
)
195 acpi_handle handle
= device
->handle
;
196 struct device
*errdev
;
198 unsigned long long sta
;
200 /* If there is no handle, the device node has been unregistered. */
202 dev_dbg(&device
->dev
, "ACPI handle missing\n");
203 put_device(&device
->dev
);
208 * Carry out two passes here and ignore errors in the first pass,
209 * because if the devices in question are memory blocks and
210 * CONFIG_MEMCG is set, one of the blocks may hold data structures
211 * that the other blocks depend on, but it is not known in advance which
214 * If the first pass is successful, the second one isn't needed, though.
217 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
218 NULL
, acpi_bus_offline_companions
,
219 (void *)false, (void **)&errdev
);
220 acpi_bus_offline_companions(handle
, 0, (void *)false, (void **)&errdev
);
223 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
224 NULL
, acpi_bus_offline_companions
,
225 (void *)true , (void **)&errdev
);
226 if (!errdev
|| acpi_force_hot_remove
)
227 acpi_bus_offline_companions(handle
, 0, (void *)true,
230 if (errdev
&& !acpi_force_hot_remove
) {
231 dev_warn(errdev
, "Offline failed.\n");
232 acpi_bus_online_companions(handle
, 0, NULL
, NULL
);
233 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
,
235 acpi_bus_online_companions
, NULL
,
237 put_device(&device
->dev
);
242 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
243 "Hot-removing device %s...\n", dev_name(&device
->dev
)));
245 acpi_bus_trim(device
);
247 /* Device node has been unregistered. */
248 put_device(&device
->dev
);
251 acpi_evaluate_lck(handle
, 0);
255 status
= acpi_evaluate_ej0(handle
);
256 if (status
== AE_NOT_FOUND
)
258 else if (ACPI_FAILURE(status
))
262 * Verify if eject was indeed successful. If not, log an error
263 * message. No need to call _OST since _EJ0 call was made OK.
265 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
266 if (ACPI_FAILURE(status
)) {
267 acpi_handle_warn(handle
,
268 "Status check after eject failed (0x%x)\n", status
);
269 } else if (sta
& ACPI_STA_DEVICE_ENABLED
) {
270 acpi_handle_warn(handle
,
271 "Eject incomplete - status 0x%llx\n", sta
);
277 static void acpi_bus_device_eject(void *context
)
279 acpi_handle handle
= context
;
280 struct acpi_device
*device
= NULL
;
281 struct acpi_scan_handler
*handler
;
282 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
285 lock_device_hotplug();
286 mutex_lock(&acpi_scan_lock
);
288 acpi_bus_get_device(handle
, &device
);
292 handler
= device
->handler
;
293 if (!handler
|| !handler
->hotplug
.enabled
) {
294 ost_code
= ACPI_OST_SC_EJECT_NOT_SUPPORTED
;
297 acpi_evaluate_hotplug_ost(handle
, ACPI_NOTIFY_EJECT_REQUEST
,
298 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
299 if (handler
->hotplug
.mode
== AHM_CONTAINER
)
300 kobject_uevent(&device
->dev
.kobj
, KOBJ_OFFLINE
);
302 get_device(&device
->dev
);
303 error
= acpi_scan_hot_remove(device
);
308 mutex_unlock(&acpi_scan_lock
);
309 unlock_device_hotplug();
313 acpi_evaluate_hotplug_ost(handle
, ACPI_NOTIFY_EJECT_REQUEST
, ost_code
,
318 static void acpi_scan_bus_device_check(acpi_handle handle
, u32 ost_source
)
320 struct acpi_device
*device
= NULL
;
321 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
324 lock_device_hotplug();
325 mutex_lock(&acpi_scan_lock
);
327 if (ost_source
!= ACPI_NOTIFY_BUS_CHECK
) {
328 acpi_bus_get_device(handle
, &device
);
330 dev_warn(&device
->dev
, "Attempt to re-insert\n");
334 error
= acpi_bus_scan(handle
);
336 acpi_handle_warn(handle
, "Namespace scan failure\n");
339 error
= acpi_bus_get_device(handle
, &device
);
341 acpi_handle_warn(handle
, "Missing device node object\n");
344 ost_code
= ACPI_OST_SC_SUCCESS
;
345 if (device
->handler
&& device
->handler
->hotplug
.mode
== AHM_CONTAINER
)
346 kobject_uevent(&device
->dev
.kobj
, KOBJ_ONLINE
);
349 acpi_evaluate_hotplug_ost(handle
, ost_source
, ost_code
, NULL
);
350 mutex_unlock(&acpi_scan_lock
);
351 unlock_device_hotplug();
354 static void acpi_scan_bus_check(void *context
)
356 acpi_scan_bus_device_check((acpi_handle
)context
,
357 ACPI_NOTIFY_BUS_CHECK
);
360 static void acpi_scan_device_check(void *context
)
362 acpi_scan_bus_device_check((acpi_handle
)context
,
363 ACPI_NOTIFY_DEVICE_CHECK
);
366 static void acpi_hotplug_unsupported(acpi_handle handle
, u32 type
)
371 case ACPI_NOTIFY_BUS_CHECK
:
372 acpi_handle_debug(handle
,
373 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
374 ost_status
= ACPI_OST_SC_INSERT_NOT_SUPPORTED
;
376 case ACPI_NOTIFY_DEVICE_CHECK
:
377 acpi_handle_debug(handle
,
378 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
379 ost_status
= ACPI_OST_SC_INSERT_NOT_SUPPORTED
;
381 case ACPI_NOTIFY_EJECT_REQUEST
:
382 acpi_handle_debug(handle
,
383 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
384 ost_status
= ACPI_OST_SC_EJECT_NOT_SUPPORTED
;
387 /* non-hotplug event; possibly handled by other handler */
391 acpi_evaluate_hotplug_ost(handle
, type
, ost_status
, NULL
);
394 static void acpi_hotplug_notify_cb(acpi_handle handle
, u32 type
, void *data
)
396 acpi_osd_exec_callback callback
;
397 struct acpi_scan_handler
*handler
= data
;
400 if (!handler
->hotplug
.enabled
)
401 return acpi_hotplug_unsupported(handle
, type
);
404 case ACPI_NOTIFY_BUS_CHECK
:
405 acpi_handle_debug(handle
, "ACPI_NOTIFY_BUS_CHECK event\n");
406 callback
= acpi_scan_bus_check
;
408 case ACPI_NOTIFY_DEVICE_CHECK
:
409 acpi_handle_debug(handle
, "ACPI_NOTIFY_DEVICE_CHECK event\n");
410 callback
= acpi_scan_device_check
;
412 case ACPI_NOTIFY_EJECT_REQUEST
:
413 acpi_handle_debug(handle
, "ACPI_NOTIFY_EJECT_REQUEST event\n");
414 callback
= acpi_bus_device_eject
;
417 /* non-hotplug event; possibly handled by other handler */
420 status
= acpi_os_hotplug_execute(callback
, handle
);
421 if (ACPI_FAILURE(status
))
422 acpi_evaluate_hotplug_ost(handle
, type
,
423 ACPI_OST_SC_NON_SPECIFIC_FAILURE
,
428 * acpi_bus_hot_remove_device: hot-remove a device and its children
429 * @context: struct acpi_eject_event pointer (freed in this func)
431 * Hot-remove a device and its children. This function frees up the
432 * memory space passed by arg context, so that the caller may call
433 * this function asynchronously through acpi_os_hotplug_execute().
435 void acpi_bus_hot_remove_device(void *context
)
437 struct acpi_eject_event
*ej_event
= context
;
438 struct acpi_device
*device
= ej_event
->device
;
439 acpi_handle handle
= device
->handle
;
442 lock_device_hotplug();
443 mutex_lock(&acpi_scan_lock
);
445 error
= acpi_scan_hot_remove(device
);
447 acpi_evaluate_hotplug_ost(handle
, ej_event
->event
,
448 ACPI_OST_SC_NON_SPECIFIC_FAILURE
,
451 mutex_unlock(&acpi_scan_lock
);
452 unlock_device_hotplug();
455 EXPORT_SYMBOL(acpi_bus_hot_remove_device
);
457 static ssize_t
real_power_state_show(struct device
*dev
,
458 struct device_attribute
*attr
, char *buf
)
460 struct acpi_device
*adev
= to_acpi_device(dev
);
464 ret
= acpi_device_get_power(adev
, &state
);
468 return sprintf(buf
, "%s\n", acpi_power_state_string(state
));
471 static DEVICE_ATTR(real_power_state
, 0444, real_power_state_show
, NULL
);
473 static ssize_t
power_state_show(struct device
*dev
,
474 struct device_attribute
*attr
, char *buf
)
476 struct acpi_device
*adev
= to_acpi_device(dev
);
478 return sprintf(buf
, "%s\n", acpi_power_state_string(adev
->power
.state
));
481 static DEVICE_ATTR(power_state
, 0444, power_state_show
, NULL
);
484 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
485 const char *buf
, size_t count
)
487 struct acpi_device
*acpi_device
= to_acpi_device(d
);
488 struct acpi_eject_event
*ej_event
;
489 acpi_object_type not_used
;
493 if (!count
|| buf
[0] != '1')
496 if ((!acpi_device
->handler
|| !acpi_device
->handler
->hotplug
.enabled
)
497 && !acpi_device
->driver
)
500 status
= acpi_get_type(acpi_device
->handle
, ¬_used
);
501 if (ACPI_FAILURE(status
) || !acpi_device
->flags
.ejectable
)
504 ej_event
= kmalloc(sizeof(*ej_event
), GFP_KERNEL
);
509 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
510 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
511 ej_event
->device
= acpi_device
;
512 ej_event
->event
= ACPI_OST_EC_OSPM_EJECT
;
513 get_device(&acpi_device
->dev
);
514 status
= acpi_os_hotplug_execute(acpi_bus_hot_remove_device
, ej_event
);
515 if (ACPI_SUCCESS(status
))
518 put_device(&acpi_device
->dev
);
520 ret
= status
== AE_NO_MEMORY
? -ENOMEM
: -EAGAIN
;
523 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
524 ACPI_OST_SC_NON_SPECIFIC_FAILURE
, NULL
);
528 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
531 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
532 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
534 return sprintf(buf
, "%s\n", acpi_device_hid(acpi_dev
));
536 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
538 static ssize_t
acpi_device_uid_show(struct device
*dev
,
539 struct device_attribute
*attr
, char *buf
)
541 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
543 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.unique_id
);
545 static DEVICE_ATTR(uid
, 0444, acpi_device_uid_show
, NULL
);
547 static ssize_t
acpi_device_adr_show(struct device
*dev
,
548 struct device_attribute
*attr
, char *buf
)
550 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
552 return sprintf(buf
, "0x%08x\n",
553 (unsigned int)(acpi_dev
->pnp
.bus_address
));
555 static DEVICE_ATTR(adr
, 0444, acpi_device_adr_show
, NULL
);
558 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
559 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
560 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
563 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
567 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
572 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
574 /* sysfs file that shows description text from the ACPI _STR method */
575 static ssize_t
description_show(struct device
*dev
,
576 struct device_attribute
*attr
,
578 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
581 if (acpi_dev
->pnp
.str_obj
== NULL
)
585 * The _STR object contains a Unicode identifier for a device.
586 * We need to convert to utf-8 so it can be displayed.
588 result
= utf16s_to_utf8s(
589 (wchar_t *)acpi_dev
->pnp
.str_obj
->buffer
.pointer
,
590 acpi_dev
->pnp
.str_obj
->buffer
.length
,
591 UTF16_LITTLE_ENDIAN
, buf
,
594 buf
[result
++] = '\n';
598 static DEVICE_ATTR(description
, 0444, description_show
, NULL
);
601 acpi_device_sun_show(struct device
*dev
, struct device_attribute
*attr
,
603 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
605 return sprintf(buf
, "%lu\n", acpi_dev
->pnp
.sun
);
607 static DEVICE_ATTR(sun
, 0444, acpi_device_sun_show
, NULL
);
609 static int acpi_device_setup_files(struct acpi_device
*dev
)
611 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
613 unsigned long long sun
;
617 * Devices gotten from FADT don't have a "path" attribute
620 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
625 if (!list_empty(&dev
->pnp
.ids
)) {
626 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
630 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
636 * If device has _STR, 'description' file is created
638 if (acpi_has_method(dev
->handle
, "_STR")) {
639 status
= acpi_evaluate_object(dev
->handle
, "_STR",
641 if (ACPI_FAILURE(status
))
642 buffer
.pointer
= NULL
;
643 dev
->pnp
.str_obj
= buffer
.pointer
;
644 result
= device_create_file(&dev
->dev
, &dev_attr_description
);
649 if (dev
->pnp
.type
.bus_address
)
650 result
= device_create_file(&dev
->dev
, &dev_attr_adr
);
651 if (dev
->pnp
.unique_id
)
652 result
= device_create_file(&dev
->dev
, &dev_attr_uid
);
654 status
= acpi_evaluate_integer(dev
->handle
, "_SUN", NULL
, &sun
);
655 if (ACPI_SUCCESS(status
)) {
656 dev
->pnp
.sun
= (unsigned long)sun
;
657 result
= device_create_file(&dev
->dev
, &dev_attr_sun
);
661 dev
->pnp
.sun
= (unsigned long)-1;
665 * If device has _EJ0, 'eject' file is created that is used to trigger
666 * hot-removal function from userland.
668 if (acpi_has_method(dev
->handle
, "_EJ0")) {
669 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
674 if (dev
->flags
.power_manageable
) {
675 result
= device_create_file(&dev
->dev
, &dev_attr_power_state
);
679 if (dev
->power
.flags
.power_resources
)
680 result
= device_create_file(&dev
->dev
,
681 &dev_attr_real_power_state
);
688 static void acpi_device_remove_files(struct acpi_device
*dev
)
690 if (dev
->flags
.power_manageable
) {
691 device_remove_file(&dev
->dev
, &dev_attr_power_state
);
692 if (dev
->power
.flags
.power_resources
)
693 device_remove_file(&dev
->dev
,
694 &dev_attr_real_power_state
);
698 * If device has _STR, remove 'description' file
700 if (acpi_has_method(dev
->handle
, "_STR")) {
701 kfree(dev
->pnp
.str_obj
);
702 device_remove_file(&dev
->dev
, &dev_attr_description
);
705 * If device has _EJ0, remove 'eject' file.
707 if (acpi_has_method(dev
->handle
, "_EJ0"))
708 device_remove_file(&dev
->dev
, &dev_attr_eject
);
710 if (acpi_has_method(dev
->handle
, "_SUN"))
711 device_remove_file(&dev
->dev
, &dev_attr_sun
);
713 if (dev
->pnp
.unique_id
)
714 device_remove_file(&dev
->dev
, &dev_attr_uid
);
715 if (dev
->pnp
.type
.bus_address
)
716 device_remove_file(&dev
->dev
, &dev_attr_adr
);
717 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
718 device_remove_file(&dev
->dev
, &dev_attr_hid
);
720 device_remove_file(&dev
->dev
, &dev_attr_path
);
722 /* --------------------------------------------------------------------------
724 -------------------------------------------------------------------------- */
726 static const struct acpi_device_id
*__acpi_match_device(
727 struct acpi_device
*device
, const struct acpi_device_id
*ids
)
729 const struct acpi_device_id
*id
;
730 struct acpi_hardware_id
*hwid
;
733 * If the device is not present, it is unnecessary to load device
736 if (!device
->status
.present
)
739 for (id
= ids
; id
->id
[0]; id
++)
740 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
)
741 if (!strcmp((char *) id
->id
, hwid
->id
))
748 * acpi_match_device - Match a struct device against a given list of ACPI IDs
749 * @ids: Array of struct acpi_device_id object to match against.
750 * @dev: The device structure to match.
752 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
753 * object for that handle and use that object to match against a given list of
756 * Return a pointer to the first matching ID on success or %NULL on failure.
758 const struct acpi_device_id
*acpi_match_device(const struct acpi_device_id
*ids
,
759 const struct device
*dev
)
761 struct acpi_device
*adev
;
762 acpi_handle handle
= ACPI_HANDLE(dev
);
764 if (!ids
|| !handle
|| acpi_bus_get_device(handle
, &adev
))
767 return __acpi_match_device(adev
, ids
);
769 EXPORT_SYMBOL_GPL(acpi_match_device
);
771 int acpi_match_device_ids(struct acpi_device
*device
,
772 const struct acpi_device_id
*ids
)
774 return __acpi_match_device(device
, ids
) ? 0 : -ENOENT
;
776 EXPORT_SYMBOL(acpi_match_device_ids
);
778 static void acpi_free_power_resources_lists(struct acpi_device
*device
)
782 if (device
->wakeup
.flags
.valid
)
783 acpi_power_resources_list_free(&device
->wakeup
.resources
);
785 if (!device
->flags
.power_manageable
)
788 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++) {
789 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
790 acpi_power_resources_list_free(&ps
->resources
);
794 static void acpi_device_release(struct device
*dev
)
796 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
798 acpi_free_pnp_ids(&acpi_dev
->pnp
);
799 acpi_free_power_resources_lists(acpi_dev
);
803 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
805 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
806 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
808 return acpi_dev
->flags
.match_driver
809 && !acpi_match_device_ids(acpi_dev
, acpi_drv
->ids
);
812 static int acpi_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
814 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
817 if (list_empty(&acpi_dev
->pnp
.ids
))
820 if (add_uevent_var(env
, "MODALIAS="))
822 len
= create_modalias(acpi_dev
, &env
->buf
[env
->buflen
- 1],
823 sizeof(env
->buf
) - env
->buflen
);
824 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
830 static void acpi_device_notify(acpi_handle handle
, u32 event
, void *data
)
832 struct acpi_device
*device
= data
;
834 device
->driver
->ops
.notify(device
, event
);
837 static acpi_status
acpi_device_notify_fixed(void *data
)
839 struct acpi_device
*device
= data
;
841 /* Fixed hardware devices have no handles */
842 acpi_device_notify(NULL
, ACPI_FIXED_HARDWARE_EVENT
, device
);
846 static int acpi_device_install_notify_handler(struct acpi_device
*device
)
850 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
852 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
853 acpi_device_notify_fixed
,
855 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
857 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
858 acpi_device_notify_fixed
,
861 status
= acpi_install_notify_handler(device
->handle
,
866 if (ACPI_FAILURE(status
))
871 static void acpi_device_remove_notify_handler(struct acpi_device
*device
)
873 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
874 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
875 acpi_device_notify_fixed
);
876 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
877 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
878 acpi_device_notify_fixed
);
880 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
884 static int acpi_device_probe(struct device
*dev
)
886 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
887 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
890 if (acpi_dev
->handler
)
893 if (!acpi_drv
->ops
.add
)
896 ret
= acpi_drv
->ops
.add(acpi_dev
);
900 acpi_dev
->driver
= acpi_drv
;
901 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
902 "Driver [%s] successfully bound to device [%s]\n",
903 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
905 if (acpi_drv
->ops
.notify
) {
906 ret
= acpi_device_install_notify_handler(acpi_dev
);
908 if (acpi_drv
->ops
.remove
)
909 acpi_drv
->ops
.remove(acpi_dev
);
911 acpi_dev
->driver
= NULL
;
912 acpi_dev
->driver_data
= NULL
;
917 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found driver [%s] for device [%s]\n",
918 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
923 static int acpi_device_remove(struct device
* dev
)
925 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
926 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
929 if (acpi_drv
->ops
.notify
)
930 acpi_device_remove_notify_handler(acpi_dev
);
931 if (acpi_drv
->ops
.remove
)
932 acpi_drv
->ops
.remove(acpi_dev
);
934 acpi_dev
->driver
= NULL
;
935 acpi_dev
->driver_data
= NULL
;
941 struct bus_type acpi_bus_type
= {
943 .match
= acpi_bus_match
,
944 .probe
= acpi_device_probe
,
945 .remove
= acpi_device_remove
,
946 .uevent
= acpi_device_uevent
,
949 static void acpi_bus_data_handler(acpi_handle handle
, void *context
)
951 /* Intentionally empty. */
954 int acpi_bus_get_device(acpi_handle handle
, struct acpi_device
**device
)
961 status
= acpi_get_data(handle
, acpi_bus_data_handler
, (void **)device
);
962 if (ACPI_FAILURE(status
) || !*device
) {
963 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
969 EXPORT_SYMBOL(acpi_bus_get_device
);
971 int acpi_device_add(struct acpi_device
*device
,
972 void (*release
)(struct device
*))
975 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
978 if (device
->handle
) {
981 status
= acpi_attach_data(device
->handle
, acpi_bus_data_handler
,
983 if (ACPI_FAILURE(status
)) {
984 acpi_handle_err(device
->handle
,
985 "Unable to attach device data\n");
993 * Link this device to its parent and siblings.
995 INIT_LIST_HEAD(&device
->children
);
996 INIT_LIST_HEAD(&device
->node
);
997 INIT_LIST_HEAD(&device
->wakeup_list
);
998 INIT_LIST_HEAD(&device
->physical_node_list
);
999 mutex_init(&device
->physical_node_lock
);
1001 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
1003 pr_err(PREFIX
"Memory allocation error\n");
1008 mutex_lock(&acpi_device_lock
);
1010 * Find suitable bus_id and instance number in acpi_bus_id_list
1011 * If failed, create one and link it into acpi_bus_id_list
1013 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
1014 if (!strcmp(acpi_device_bus_id
->bus_id
,
1015 acpi_device_hid(device
))) {
1016 acpi_device_bus_id
->instance_no
++;
1023 acpi_device_bus_id
= new_bus_id
;
1024 strcpy(acpi_device_bus_id
->bus_id
, acpi_device_hid(device
));
1025 acpi_device_bus_id
->instance_no
= 0;
1026 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
1028 dev_set_name(&device
->dev
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
1031 list_add_tail(&device
->node
, &device
->parent
->children
);
1033 if (device
->wakeup
.flags
.valid
)
1034 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
1035 mutex_unlock(&acpi_device_lock
);
1038 device
->dev
.parent
= &device
->parent
->dev
;
1039 device
->dev
.bus
= &acpi_bus_type
;
1040 device
->dev
.release
= release
;
1041 result
= device_add(&device
->dev
);
1043 dev_err(&device
->dev
, "Error registering device\n");
1047 result
= acpi_device_setup_files(device
);
1049 printk(KERN_ERR PREFIX
"Error creating sysfs interface for device %s\n",
1050 dev_name(&device
->dev
));
1055 mutex_lock(&acpi_device_lock
);
1057 list_del(&device
->node
);
1058 list_del(&device
->wakeup_list
);
1059 mutex_unlock(&acpi_device_lock
);
1062 acpi_detach_data(device
->handle
, acpi_bus_data_handler
);
1066 static void acpi_device_unregister(struct acpi_device
*device
)
1068 mutex_lock(&acpi_device_lock
);
1070 list_del(&device
->node
);
1072 list_del(&device
->wakeup_list
);
1073 mutex_unlock(&acpi_device_lock
);
1075 acpi_detach_data(device
->handle
, acpi_bus_data_handler
);
1077 acpi_power_add_remove_device(device
, false);
1078 acpi_device_remove_files(device
);
1080 device
->remove(device
);
1082 device_del(&device
->dev
);
1084 * Transition the device to D3cold to drop the reference counts of all
1085 * power resources the device depends on and turn off the ones that have
1086 * no more references.
1088 acpi_device_set_power(device
, ACPI_STATE_D3_COLD
);
1089 device
->handle
= NULL
;
1090 put_device(&device
->dev
);
1093 /* --------------------------------------------------------------------------
1095 -------------------------------------------------------------------------- */
1097 * acpi_bus_register_driver - register a driver with the ACPI bus
1098 * @driver: driver being registered
1100 * Registers a driver with the ACPI bus. Searches the namespace for all
1101 * devices that match the driver's criteria and binds. Returns zero for
1102 * success or a negative error status for failure.
1104 int acpi_bus_register_driver(struct acpi_driver
*driver
)
1110 driver
->drv
.name
= driver
->name
;
1111 driver
->drv
.bus
= &acpi_bus_type
;
1112 driver
->drv
.owner
= driver
->owner
;
1114 ret
= driver_register(&driver
->drv
);
1118 EXPORT_SYMBOL(acpi_bus_register_driver
);
1121 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1122 * @driver: driver to unregister
1124 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1125 * devices that match the driver's criteria and unbinds.
1127 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
1129 driver_unregister(&driver
->drv
);
1132 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
1134 /* --------------------------------------------------------------------------
1136 -------------------------------------------------------------------------- */
1137 static struct acpi_device
*acpi_bus_get_parent(acpi_handle handle
)
1139 struct acpi_device
*device
= NULL
;
1143 * Fixed hardware devices do not appear in the namespace and do not
1144 * have handles, but we fabricate acpi_devices for them, so we have
1145 * to deal with them specially.
1151 status
= acpi_get_parent(handle
, &handle
);
1152 if (ACPI_FAILURE(status
))
1153 return status
== AE_NULL_ENTRY
? NULL
: acpi_root
;
1154 } while (acpi_bus_get_device(handle
, &device
));
1159 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
1163 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1164 union acpi_object
*obj
;
1166 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
1167 if (ACPI_FAILURE(status
))
1170 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
1171 if (ACPI_SUCCESS(status
)) {
1172 obj
= buffer
.pointer
;
1173 status
= acpi_get_handle(ACPI_ROOT_OBJECT
, obj
->string
.pointer
,
1175 kfree(buffer
.pointer
);
1179 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
1181 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle
,
1182 struct acpi_device_wakeup
*wakeup
)
1184 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1185 union acpi_object
*package
= NULL
;
1186 union acpi_object
*element
= NULL
;
1193 INIT_LIST_HEAD(&wakeup
->resources
);
1196 status
= acpi_evaluate_object(handle
, "_PRW", NULL
, &buffer
);
1197 if (ACPI_FAILURE(status
)) {
1198 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
1202 package
= (union acpi_object
*)buffer
.pointer
;
1204 if (!package
|| package
->package
.count
< 2)
1207 element
= &(package
->package
.elements
[0]);
1211 if (element
->type
== ACPI_TYPE_PACKAGE
) {
1212 if ((element
->package
.count
< 2) ||
1213 (element
->package
.elements
[0].type
!=
1214 ACPI_TYPE_LOCAL_REFERENCE
)
1215 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
1218 wakeup
->gpe_device
=
1219 element
->package
.elements
[0].reference
.handle
;
1220 wakeup
->gpe_number
=
1221 (u32
) element
->package
.elements
[1].integer
.value
;
1222 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
1223 wakeup
->gpe_device
= NULL
;
1224 wakeup
->gpe_number
= element
->integer
.value
;
1229 element
= &(package
->package
.elements
[1]);
1230 if (element
->type
!= ACPI_TYPE_INTEGER
)
1233 wakeup
->sleep_state
= element
->integer
.value
;
1235 err
= acpi_extract_power_resources(package
, 2, &wakeup
->resources
);
1239 if (!list_empty(&wakeup
->resources
)) {
1242 err
= acpi_power_wakeup_list_init(&wakeup
->resources
,
1245 acpi_handle_warn(handle
, "Retrieving current states "
1246 "of wakeup power resources failed\n");
1247 acpi_power_resources_list_free(&wakeup
->resources
);
1250 if (sleep_state
< wakeup
->sleep_state
) {
1251 acpi_handle_warn(handle
, "Overriding _PRW sleep state "
1252 "(S%d) by S%d from power resources\n",
1253 (int)wakeup
->sleep_state
, sleep_state
);
1254 wakeup
->sleep_state
= sleep_state
;
1257 acpi_setup_gpe_for_wake(handle
, wakeup
->gpe_device
, wakeup
->gpe_number
);
1260 kfree(buffer
.pointer
);
1264 static void acpi_bus_set_run_wake_flags(struct acpi_device
*device
)
1266 struct acpi_device_id button_device_ids
[] = {
1273 acpi_event_status event_status
;
1275 device
->wakeup
.flags
.notifier_present
= 0;
1277 /* Power button, Lid switch always enable wakeup */
1278 if (!acpi_match_device_ids(device
, button_device_ids
)) {
1279 device
->wakeup
.flags
.run_wake
= 1;
1280 if (!acpi_match_device_ids(device
, &button_device_ids
[1])) {
1281 /* Do not use Lid/sleep button for S5 wakeup */
1282 if (device
->wakeup
.sleep_state
== ACPI_STATE_S5
)
1283 device
->wakeup
.sleep_state
= ACPI_STATE_S4
;
1285 device_set_wakeup_capable(&device
->dev
, true);
1289 status
= acpi_get_gpe_status(device
->wakeup
.gpe_device
,
1290 device
->wakeup
.gpe_number
,
1292 if (status
== AE_OK
)
1293 device
->wakeup
.flags
.run_wake
=
1294 !!(event_status
& ACPI_EVENT_FLAG_HANDLE
);
1297 static void acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
1301 /* Presence of _PRW indicates wake capable */
1302 if (!acpi_has_method(device
->handle
, "_PRW"))
1305 err
= acpi_bus_extract_wakeup_device_power_package(device
->handle
,
1308 dev_err(&device
->dev
, "_PRW evaluation error: %d\n", err
);
1312 device
->wakeup
.flags
.valid
= 1;
1313 device
->wakeup
.prepare_count
= 0;
1314 acpi_bus_set_run_wake_flags(device
);
1315 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1316 * system for the ACPI device with the _PRW object.
1317 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1318 * So it is necessary to call _DSW object first. Only when it is not
1319 * present will the _PSW object used.
1321 err
= acpi_device_sleep_wake(device
, 0, 0, 0);
1323 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1324 "error in _DSW or _PSW evaluation\n"));
1327 static void acpi_bus_init_power_state(struct acpi_device
*device
, int state
)
1329 struct acpi_device_power_state
*ps
= &device
->power
.states
[state
];
1330 char pathname
[5] = { '_', 'P', 'R', '0' + state
, '\0' };
1331 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1334 INIT_LIST_HEAD(&ps
->resources
);
1336 /* Evaluate "_PRx" to get referenced power resources */
1337 status
= acpi_evaluate_object(device
->handle
, pathname
, NULL
, &buffer
);
1338 if (ACPI_SUCCESS(status
)) {
1339 union acpi_object
*package
= buffer
.pointer
;
1341 if (buffer
.length
&& package
1342 && package
->type
== ACPI_TYPE_PACKAGE
1343 && package
->package
.count
) {
1344 int err
= acpi_extract_power_resources(package
, 0,
1347 device
->power
.flags
.power_resources
= 1;
1349 ACPI_FREE(buffer
.pointer
);
1352 /* Evaluate "_PSx" to see if we can do explicit sets */
1354 if (acpi_has_method(device
->handle
, pathname
))
1355 ps
->flags
.explicit_set
= 1;
1358 * State is valid if there are means to put the device into it.
1359 * D3hot is only valid if _PR3 present.
1361 if (!list_empty(&ps
->resources
)
1362 || (ps
->flags
.explicit_set
&& state
< ACPI_STATE_D3_HOT
)) {
1363 ps
->flags
.valid
= 1;
1364 ps
->flags
.os_accessible
= 1;
1367 ps
->power
= -1; /* Unknown - driver assigned */
1368 ps
->latency
= -1; /* Unknown - driver assigned */
1371 static void acpi_bus_get_power_flags(struct acpi_device
*device
)
1375 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1376 if (!acpi_has_method(device
->handle
, "_PS0") &&
1377 !acpi_has_method(device
->handle
, "_PR0"))
1380 device
->flags
.power_manageable
= 1;
1383 * Power Management Flags
1385 if (acpi_has_method(device
->handle
, "_PSC"))
1386 device
->power
.flags
.explicit_get
= 1;
1387 if (acpi_has_method(device
->handle
, "_IRC"))
1388 device
->power
.flags
.inrush_current
= 1;
1391 * Enumerate supported power management states
1393 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++)
1394 acpi_bus_init_power_state(device
, i
);
1396 INIT_LIST_HEAD(&device
->power
.states
[ACPI_STATE_D3_COLD
].resources
);
1398 /* Set defaults for D0 and D3 states (always valid) */
1399 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
1400 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
1401 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.valid
= 1;
1402 device
->power
.states
[ACPI_STATE_D3_COLD
].power
= 0;
1404 /* Set D3cold's explicit_set flag if _PS3 exists. */
1405 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
)
1406 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.explicit_set
= 1;
1408 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1409 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
||
1410 device
->power
.flags
.power_resources
)
1411 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.os_accessible
= 1;
1413 if (acpi_bus_init_power(device
)) {
1414 acpi_free_power_resources_lists(device
);
1415 device
->flags
.power_manageable
= 0;
1419 static void acpi_bus_get_flags(struct acpi_device
*device
)
1421 /* Presence of _STA indicates 'dynamic_status' */
1422 if (acpi_has_method(device
->handle
, "_STA"))
1423 device
->flags
.dynamic_status
= 1;
1425 /* Presence of _RMV indicates 'removable' */
1426 if (acpi_has_method(device
->handle
, "_RMV"))
1427 device
->flags
.removable
= 1;
1429 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1430 if (acpi_has_method(device
->handle
, "_EJD") ||
1431 acpi_has_method(device
->handle
, "_EJ0"))
1432 device
->flags
.ejectable
= 1;
1435 static void acpi_device_get_busid(struct acpi_device
*device
)
1437 char bus_id
[5] = { '?', 0 };
1438 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
1444 * The device's Bus ID is simply the object name.
1445 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1447 if (ACPI_IS_ROOT_DEVICE(device
)) {
1448 strcpy(device
->pnp
.bus_id
, "ACPI");
1452 switch (device
->device_type
) {
1453 case ACPI_BUS_TYPE_POWER_BUTTON
:
1454 strcpy(device
->pnp
.bus_id
, "PWRF");
1456 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1457 strcpy(device
->pnp
.bus_id
, "SLPF");
1460 acpi_get_name(device
->handle
, ACPI_SINGLE_NAME
, &buffer
);
1461 /* Clean up trailing underscores (if any) */
1462 for (i
= 3; i
> 1; i
--) {
1463 if (bus_id
[i
] == '_')
1468 strcpy(device
->pnp
.bus_id
, bus_id
);
1474 * acpi_ata_match - see if an acpi object is an ATA device
1476 * If an acpi object has one of the ACPI ATA methods defined,
1477 * then we can safely call it an ATA device.
1479 bool acpi_ata_match(acpi_handle handle
)
1481 return acpi_has_method(handle
, "_GTF") ||
1482 acpi_has_method(handle
, "_GTM") ||
1483 acpi_has_method(handle
, "_STM") ||
1484 acpi_has_method(handle
, "_SDD");
1488 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1490 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1491 * then we can safely call it an ejectable drive bay
1493 bool acpi_bay_match(acpi_handle handle
)
1495 acpi_handle phandle
;
1497 if (!acpi_has_method(handle
, "_EJ0"))
1499 if (acpi_ata_match(handle
))
1501 if (ACPI_FAILURE(acpi_get_parent(handle
, &phandle
)))
1504 return acpi_ata_match(phandle
);
1508 * acpi_dock_match - see if an acpi object has a _DCK method
1510 bool acpi_dock_match(acpi_handle handle
)
1512 return acpi_has_method(handle
, "_DCK");
1515 const char *acpi_device_hid(struct acpi_device
*device
)
1517 struct acpi_hardware_id
*hid
;
1519 if (list_empty(&device
->pnp
.ids
))
1522 hid
= list_first_entry(&device
->pnp
.ids
, struct acpi_hardware_id
, list
);
1525 EXPORT_SYMBOL(acpi_device_hid
);
1527 static void acpi_add_id(struct acpi_device_pnp
*pnp
, const char *dev_id
)
1529 struct acpi_hardware_id
*id
;
1531 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
1535 id
->id
= kstrdup(dev_id
, GFP_KERNEL
);
1541 list_add_tail(&id
->list
, &pnp
->ids
);
1542 pnp
->type
.hardware_id
= 1;
1546 * Old IBM workstations have a DSDT bug wherein the SMBus object
1547 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1548 * prefix. Work around this.
1550 static bool acpi_ibm_smbus_match(acpi_handle handle
)
1552 char node_name
[ACPI_PATH_SEGMENT_LENGTH
];
1553 struct acpi_buffer path
= { sizeof(node_name
), node_name
};
1555 if (!dmi_name_in_vendors("IBM"))
1558 /* Look for SMBS object */
1559 if (ACPI_FAILURE(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &path
)) ||
1560 strcmp("SMBS", path
.pointer
))
1563 /* Does it have the necessary (but misnamed) methods? */
1564 if (acpi_has_method(handle
, "SBI") &&
1565 acpi_has_method(handle
, "SBR") &&
1566 acpi_has_method(handle
, "SBW"))
1572 static void acpi_set_pnp_ids(acpi_handle handle
, struct acpi_device_pnp
*pnp
,
1576 struct acpi_device_info
*info
;
1577 struct acpi_pnp_device_id_list
*cid_list
;
1580 switch (device_type
) {
1581 case ACPI_BUS_TYPE_DEVICE
:
1582 if (handle
== ACPI_ROOT_OBJECT
) {
1583 acpi_add_id(pnp
, ACPI_SYSTEM_HID
);
1587 status
= acpi_get_object_info(handle
, &info
);
1588 if (ACPI_FAILURE(status
)) {
1589 pr_err(PREFIX
"%s: Error reading device info\n",
1594 if (info
->valid
& ACPI_VALID_HID
)
1595 acpi_add_id(pnp
, info
->hardware_id
.string
);
1596 if (info
->valid
& ACPI_VALID_CID
) {
1597 cid_list
= &info
->compatible_id_list
;
1598 for (i
= 0; i
< cid_list
->count
; i
++)
1599 acpi_add_id(pnp
, cid_list
->ids
[i
].string
);
1601 if (info
->valid
& ACPI_VALID_ADR
) {
1602 pnp
->bus_address
= info
->address
;
1603 pnp
->type
.bus_address
= 1;
1605 if (info
->valid
& ACPI_VALID_UID
)
1606 pnp
->unique_id
= kstrdup(info
->unique_id
.string
,
1612 * Some devices don't reliably have _HIDs & _CIDs, so add
1613 * synthetic HIDs to make sure drivers can find them.
1615 if (acpi_is_video_device(handle
))
1616 acpi_add_id(pnp
, ACPI_VIDEO_HID
);
1617 else if (acpi_bay_match(handle
))
1618 acpi_add_id(pnp
, ACPI_BAY_HID
);
1619 else if (acpi_dock_match(handle
))
1620 acpi_add_id(pnp
, ACPI_DOCK_HID
);
1621 else if (acpi_ibm_smbus_match(handle
))
1622 acpi_add_id(pnp
, ACPI_SMBUS_IBM_HID
);
1623 else if (list_empty(&pnp
->ids
) && handle
== ACPI_ROOT_OBJECT
) {
1624 acpi_add_id(pnp
, ACPI_BUS_HID
); /* \_SB, LNXSYBUS */
1625 strcpy(pnp
->device_name
, ACPI_BUS_DEVICE_NAME
);
1626 strcpy(pnp
->device_class
, ACPI_BUS_CLASS
);
1630 case ACPI_BUS_TYPE_POWER
:
1631 acpi_add_id(pnp
, ACPI_POWER_HID
);
1633 case ACPI_BUS_TYPE_PROCESSOR
:
1634 acpi_add_id(pnp
, ACPI_PROCESSOR_OBJECT_HID
);
1636 case ACPI_BUS_TYPE_THERMAL
:
1637 acpi_add_id(pnp
, ACPI_THERMAL_HID
);
1639 case ACPI_BUS_TYPE_POWER_BUTTON
:
1640 acpi_add_id(pnp
, ACPI_BUTTON_HID_POWERF
);
1642 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1643 acpi_add_id(pnp
, ACPI_BUTTON_HID_SLEEPF
);
1648 void acpi_free_pnp_ids(struct acpi_device_pnp
*pnp
)
1650 struct acpi_hardware_id
*id
, *tmp
;
1652 list_for_each_entry_safe(id
, tmp
, &pnp
->ids
, list
) {
1656 kfree(pnp
->unique_id
);
1659 void acpi_init_device_object(struct acpi_device
*device
, acpi_handle handle
,
1660 int type
, unsigned long long sta
)
1662 INIT_LIST_HEAD(&device
->pnp
.ids
);
1663 device
->device_type
= type
;
1664 device
->handle
= handle
;
1665 device
->parent
= acpi_bus_get_parent(handle
);
1666 STRUCT_TO_INT(device
->status
) = sta
;
1667 acpi_device_get_busid(device
);
1668 acpi_set_pnp_ids(handle
, &device
->pnp
, type
);
1669 acpi_bus_get_flags(device
);
1670 device
->flags
.match_driver
= false;
1671 device_initialize(&device
->dev
);
1672 dev_set_uevent_suppress(&device
->dev
, true);
1675 void acpi_device_add_finalize(struct acpi_device
*device
)
1677 device
->flags
.match_driver
= true;
1678 dev_set_uevent_suppress(&device
->dev
, false);
1679 kobject_uevent(&device
->dev
.kobj
, KOBJ_ADD
);
1682 static int acpi_add_single_object(struct acpi_device
**child
,
1683 acpi_handle handle
, int type
,
1684 unsigned long long sta
)
1687 struct acpi_device
*device
;
1688 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1690 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
1692 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1696 acpi_init_device_object(device
, handle
, type
, sta
);
1697 acpi_bus_get_power_flags(device
);
1698 acpi_bus_get_wakeup_device_flags(device
);
1700 result
= acpi_device_add(device
, acpi_device_release
);
1702 acpi_device_release(&device
->dev
);
1706 acpi_power_add_remove_device(device
, true);
1707 acpi_device_add_finalize(device
);
1708 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1709 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Added %s [%s] parent %s\n",
1710 dev_name(&device
->dev
), (char *) buffer
.pointer
,
1711 device
->parent
? dev_name(&device
->parent
->dev
) : "(null)"));
1712 kfree(buffer
.pointer
);
1717 static int acpi_bus_type_and_status(acpi_handle handle
, int *type
,
1718 unsigned long long *sta
)
1721 acpi_object_type acpi_type
;
1723 status
= acpi_get_type(handle
, &acpi_type
);
1724 if (ACPI_FAILURE(status
))
1727 switch (acpi_type
) {
1728 case ACPI_TYPE_ANY
: /* for ACPI_ROOT_OBJECT */
1729 case ACPI_TYPE_DEVICE
:
1730 *type
= ACPI_BUS_TYPE_DEVICE
;
1731 status
= acpi_bus_get_status_handle(handle
, sta
);
1732 if (ACPI_FAILURE(status
))
1735 case ACPI_TYPE_PROCESSOR
:
1736 *type
= ACPI_BUS_TYPE_PROCESSOR
;
1737 status
= acpi_bus_get_status_handle(handle
, sta
);
1738 if (ACPI_FAILURE(status
))
1741 case ACPI_TYPE_THERMAL
:
1742 *type
= ACPI_BUS_TYPE_THERMAL
;
1743 *sta
= ACPI_STA_DEFAULT
;
1745 case ACPI_TYPE_POWER
:
1746 *type
= ACPI_BUS_TYPE_POWER
;
1747 *sta
= ACPI_STA_DEFAULT
;
1756 static bool acpi_scan_handler_matching(struct acpi_scan_handler
*handler
,
1758 const struct acpi_device_id
**matchid
)
1760 const struct acpi_device_id
*devid
;
1762 for (devid
= handler
->ids
; devid
->id
[0]; devid
++)
1763 if (!strcmp((char *)devid
->id
, idstr
)) {
1773 static struct acpi_scan_handler
*acpi_scan_match_handler(char *idstr
,
1774 const struct acpi_device_id
**matchid
)
1776 struct acpi_scan_handler
*handler
;
1778 list_for_each_entry(handler
, &acpi_scan_handlers_list
, list_node
)
1779 if (acpi_scan_handler_matching(handler
, idstr
, matchid
))
1785 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile
*hotplug
, bool val
)
1787 if (!!hotplug
->enabled
== !!val
)
1790 mutex_lock(&acpi_scan_lock
);
1792 hotplug
->enabled
= val
;
1794 mutex_unlock(&acpi_scan_lock
);
1797 static void acpi_scan_init_hotplug(acpi_handle handle
, int type
)
1799 struct acpi_device_pnp pnp
= {};
1800 struct acpi_hardware_id
*hwid
;
1801 struct acpi_scan_handler
*handler
;
1803 INIT_LIST_HEAD(&pnp
.ids
);
1804 acpi_set_pnp_ids(handle
, &pnp
, type
);
1806 if (!pnp
.type
.hardware_id
)
1810 * This relies on the fact that acpi_install_notify_handler() will not
1811 * install the same notify handler routine twice for the same handle.
1813 list_for_each_entry(hwid
, &pnp
.ids
, list
) {
1814 handler
= acpi_scan_match_handler(hwid
->id
, NULL
);
1815 if (handler
&& !handler
->hotplug
.ignore
) {
1816 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1817 acpi_hotplug_notify_cb
, handler
);
1823 acpi_free_pnp_ids(&pnp
);
1826 static acpi_status
acpi_bus_check_add(acpi_handle handle
, u32 lvl_not_used
,
1827 void *not_used
, void **return_value
)
1829 struct acpi_device
*device
= NULL
;
1831 unsigned long long sta
;
1834 acpi_bus_get_device(handle
, &device
);
1838 result
= acpi_bus_type_and_status(handle
, &type
, &sta
);
1842 if (type
== ACPI_BUS_TYPE_POWER
) {
1843 acpi_add_power_resource(handle
);
1847 acpi_scan_init_hotplug(handle
, type
);
1849 if (!(sta
& ACPI_STA_DEVICE_PRESENT
) &&
1850 !(sta
& ACPI_STA_DEVICE_FUNCTIONING
)) {
1851 struct acpi_device_wakeup wakeup
;
1853 if (acpi_has_method(handle
, "_PRW")) {
1854 acpi_bus_extract_wakeup_device_power_package(handle
,
1856 acpi_power_resources_list_free(&wakeup
.resources
);
1858 return AE_CTRL_DEPTH
;
1861 acpi_add_single_object(&device
, handle
, type
, sta
);
1863 return AE_CTRL_DEPTH
;
1867 *return_value
= device
;
1872 static int acpi_scan_attach_handler(struct acpi_device
*device
)
1874 struct acpi_hardware_id
*hwid
;
1877 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
) {
1878 const struct acpi_device_id
*devid
;
1879 struct acpi_scan_handler
*handler
;
1881 handler
= acpi_scan_match_handler(hwid
->id
, &devid
);
1883 ret
= handler
->attach(device
, devid
);
1885 device
->handler
= handler
;
1887 } else if (ret
< 0) {
1895 static acpi_status
acpi_bus_device_attach(acpi_handle handle
, u32 lvl_not_used
,
1896 void *not_used
, void **ret_not_used
)
1898 struct acpi_device
*device
;
1899 unsigned long long sta_not_used
;
1903 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1904 * namespace walks prematurely.
1906 if (acpi_bus_type_and_status(handle
, &ret
, &sta_not_used
))
1909 if (acpi_bus_get_device(handle
, &device
))
1910 return AE_CTRL_DEPTH
;
1912 if (device
->handler
)
1915 ret
= acpi_scan_attach_handler(device
);
1917 return ret
> 0 ? AE_OK
: AE_CTRL_DEPTH
;
1919 ret
= device_attach(&device
->dev
);
1920 return ret
>= 0 ? AE_OK
: AE_CTRL_DEPTH
;
1924 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1925 * @handle: Root of the namespace scope to scan.
1927 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1930 * If no devices were found, -ENODEV is returned, but it does not mean that
1931 * there has been a real error. There just have been no suitable ACPI objects
1932 * in the table trunk from which the kernel could create a device and add an
1933 * appropriate driver.
1935 * Must be called under acpi_scan_lock.
1937 int acpi_bus_scan(acpi_handle handle
)
1939 void *device
= NULL
;
1942 if (ACPI_SUCCESS(acpi_bus_check_add(handle
, 0, NULL
, &device
)))
1943 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
1944 acpi_bus_check_add
, NULL
, NULL
, &device
);
1948 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle
, 0, NULL
, NULL
)))
1949 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
1950 acpi_bus_device_attach
, NULL
, NULL
, NULL
);
1954 EXPORT_SYMBOL(acpi_bus_scan
);
1956 static acpi_status
acpi_bus_device_detach(acpi_handle handle
, u32 lvl_not_used
,
1957 void *not_used
, void **ret_not_used
)
1959 struct acpi_device
*device
= NULL
;
1961 if (!acpi_bus_get_device(handle
, &device
)) {
1962 struct acpi_scan_handler
*dev_handler
= device
->handler
;
1965 if (dev_handler
->detach
)
1966 dev_handler
->detach(device
);
1968 device
->handler
= NULL
;
1970 device_release_driver(&device
->dev
);
1976 static acpi_status
acpi_bus_remove(acpi_handle handle
, u32 lvl_not_used
,
1977 void *not_used
, void **ret_not_used
)
1979 struct acpi_device
*device
= NULL
;
1981 if (!acpi_bus_get_device(handle
, &device
))
1982 acpi_device_unregister(device
);
1988 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1989 * @start: Root of the ACPI device nodes subtree to remove.
1991 * Must be called under acpi_scan_lock.
1993 void acpi_bus_trim(struct acpi_device
*start
)
1996 * Execute acpi_bus_device_detach() as a post-order callback to detach
1997 * all ACPI drivers from the device nodes being removed.
1999 acpi_walk_namespace(ACPI_TYPE_ANY
, start
->handle
, ACPI_UINT32_MAX
, NULL
,
2000 acpi_bus_device_detach
, NULL
, NULL
);
2001 acpi_bus_device_detach(start
->handle
, 0, NULL
, NULL
);
2003 * Execute acpi_bus_remove() as a post-order callback to remove device
2004 * nodes in the given namespace scope.
2006 acpi_walk_namespace(ACPI_TYPE_ANY
, start
->handle
, ACPI_UINT32_MAX
, NULL
,
2007 acpi_bus_remove
, NULL
, NULL
);
2008 acpi_bus_remove(start
->handle
, 0, NULL
, NULL
);
2010 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
2012 static int acpi_bus_scan_fixed(void)
2017 * Enumerate all fixed-feature devices.
2019 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
)) {
2020 struct acpi_device
*device
= NULL
;
2022 result
= acpi_add_single_object(&device
, NULL
,
2023 ACPI_BUS_TYPE_POWER_BUTTON
,
2028 result
= device_attach(&device
->dev
);
2032 device_init_wakeup(&device
->dev
, true);
2035 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
)) {
2036 struct acpi_device
*device
= NULL
;
2038 result
= acpi_add_single_object(&device
, NULL
,
2039 ACPI_BUS_TYPE_SLEEP_BUTTON
,
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_device_unregister(acpi_root
);
2088 acpi_update_all_gpes();
2090 acpi_pci_root_hp_init();
2093 mutex_unlock(&acpi_scan_lock
);