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 acpi_object_list arg_list
;
197 union acpi_object arg
;
198 struct device
*errdev
;
200 unsigned long long sta
;
202 /* If there is no handle, the device node has been unregistered. */
204 dev_dbg(&device
->dev
, "ACPI handle missing\n");
205 put_device(&device
->dev
);
209 lock_device_hotplug();
212 * Carry out two passes here and ignore errors in the first pass,
213 * because if the devices in question are memory blocks and
214 * CONFIG_MEMCG is set, one of the blocks may hold data structures
215 * that the other blocks depend on, but it is not known in advance which
218 * If the first pass is successful, the second one isn't needed, though.
221 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
222 NULL
, acpi_bus_offline_companions
,
223 (void *)false, (void **)&errdev
);
224 acpi_bus_offline_companions(handle
, 0, (void *)false, (void **)&errdev
);
227 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
228 NULL
, acpi_bus_offline_companions
,
229 (void *)true , (void **)&errdev
);
230 if (!errdev
|| acpi_force_hot_remove
)
231 acpi_bus_offline_companions(handle
, 0, (void *)true,
234 if (errdev
&& !acpi_force_hot_remove
) {
235 dev_warn(errdev
, "Offline failed.\n");
236 acpi_bus_online_companions(handle
, 0, NULL
, NULL
);
237 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
,
239 acpi_bus_online_companions
, NULL
,
242 unlock_device_hotplug();
244 put_device(&device
->dev
);
249 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
250 "Hot-removing device %s...\n", dev_name(&device
->dev
)));
252 acpi_bus_trim(device
);
254 unlock_device_hotplug();
256 /* Device node has been unregistered. */
257 put_device(&device
->dev
);
260 if (acpi_has_method(handle
, "_LCK")) {
262 arg_list
.pointer
= &arg
;
263 arg
.type
= ACPI_TYPE_INTEGER
;
264 arg
.integer
.value
= 0;
265 acpi_evaluate_object(handle
, "_LCK", &arg_list
, NULL
);
269 arg_list
.pointer
= &arg
;
270 arg
.type
= ACPI_TYPE_INTEGER
;
271 arg
.integer
.value
= 1;
276 status
= acpi_evaluate_object(handle
, "_EJ0", &arg_list
, NULL
);
277 if (ACPI_FAILURE(status
)) {
278 if (status
== AE_NOT_FOUND
) {
281 acpi_handle_warn(handle
, "Eject failed (0x%x)\n",
288 * Verify if eject was indeed successful. If not, log an error
289 * message. No need to call _OST since _EJ0 call was made OK.
291 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
292 if (ACPI_FAILURE(status
)) {
293 acpi_handle_warn(handle
,
294 "Status check after eject failed (0x%x)\n", status
);
295 } else if (sta
& ACPI_STA_DEVICE_ENABLED
) {
296 acpi_handle_warn(handle
,
297 "Eject incomplete - status 0x%llx\n", sta
);
303 static void acpi_bus_device_eject(void *context
)
305 acpi_handle handle
= context
;
306 struct acpi_device
*device
= NULL
;
307 struct acpi_scan_handler
*handler
;
308 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
310 mutex_lock(&acpi_scan_lock
);
312 acpi_bus_get_device(handle
, &device
);
316 handler
= device
->handler
;
317 if (!handler
|| !handler
->hotplug
.enabled
) {
318 ost_code
= ACPI_OST_SC_EJECT_NOT_SUPPORTED
;
321 acpi_evaluate_hotplug_ost(handle
, ACPI_NOTIFY_EJECT_REQUEST
,
322 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
323 if (handler
->hotplug
.mode
== AHM_CONTAINER
) {
324 device
->flags
.eject_pending
= true;
325 kobject_uevent(&device
->dev
.kobj
, KOBJ_OFFLINE
);
329 get_device(&device
->dev
);
330 error
= acpi_scan_hot_remove(device
);
336 mutex_unlock(&acpi_scan_lock
);
340 acpi_evaluate_hotplug_ost(handle
, ACPI_NOTIFY_EJECT_REQUEST
, ost_code
,
345 static void acpi_scan_bus_device_check(acpi_handle handle
, u32 ost_source
)
347 struct acpi_device
*device
= NULL
;
348 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
351 mutex_lock(&acpi_scan_lock
);
352 lock_device_hotplug();
354 acpi_bus_get_device(handle
, &device
);
356 dev_warn(&device
->dev
, "Attempt to re-insert\n");
359 acpi_evaluate_hotplug_ost(handle
, ost_source
,
360 ACPI_OST_SC_INSERT_IN_PROGRESS
, NULL
);
361 error
= acpi_bus_scan(handle
);
363 acpi_handle_warn(handle
, "Namespace scan failure\n");
366 error
= acpi_bus_get_device(handle
, &device
);
368 acpi_handle_warn(handle
, "Missing device node object\n");
371 ost_code
= ACPI_OST_SC_SUCCESS
;
372 if (device
->handler
&& device
->handler
->hotplug
.mode
== AHM_CONTAINER
)
373 kobject_uevent(&device
->dev
.kobj
, KOBJ_ONLINE
);
376 unlock_device_hotplug();
377 acpi_evaluate_hotplug_ost(handle
, ost_source
, ost_code
, NULL
);
378 mutex_unlock(&acpi_scan_lock
);
381 static void acpi_scan_bus_check(void *context
)
383 acpi_scan_bus_device_check((acpi_handle
)context
,
384 ACPI_NOTIFY_BUS_CHECK
);
387 static void acpi_scan_device_check(void *context
)
389 acpi_scan_bus_device_check((acpi_handle
)context
,
390 ACPI_NOTIFY_DEVICE_CHECK
);
393 static void acpi_hotplug_unsupported(acpi_handle handle
, u32 type
)
398 case ACPI_NOTIFY_BUS_CHECK
:
399 acpi_handle_debug(handle
,
400 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
401 ost_status
= ACPI_OST_SC_INSERT_NOT_SUPPORTED
;
403 case ACPI_NOTIFY_DEVICE_CHECK
:
404 acpi_handle_debug(handle
,
405 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
406 ost_status
= ACPI_OST_SC_INSERT_NOT_SUPPORTED
;
408 case ACPI_NOTIFY_EJECT_REQUEST
:
409 acpi_handle_debug(handle
,
410 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
411 ost_status
= ACPI_OST_SC_EJECT_NOT_SUPPORTED
;
414 /* non-hotplug event; possibly handled by other handler */
418 acpi_evaluate_hotplug_ost(handle
, type
, ost_status
, NULL
);
421 static void acpi_hotplug_notify_cb(acpi_handle handle
, u32 type
, void *data
)
423 acpi_osd_exec_callback callback
;
424 struct acpi_scan_handler
*handler
= data
;
427 if (!handler
->hotplug
.enabled
)
428 return acpi_hotplug_unsupported(handle
, type
);
431 case ACPI_NOTIFY_BUS_CHECK
:
432 acpi_handle_debug(handle
, "ACPI_NOTIFY_BUS_CHECK event\n");
433 callback
= acpi_scan_bus_check
;
435 case ACPI_NOTIFY_DEVICE_CHECK
:
436 acpi_handle_debug(handle
, "ACPI_NOTIFY_DEVICE_CHECK event\n");
437 callback
= acpi_scan_device_check
;
439 case ACPI_NOTIFY_EJECT_REQUEST
:
440 acpi_handle_debug(handle
, "ACPI_NOTIFY_EJECT_REQUEST event\n");
441 callback
= acpi_bus_device_eject
;
444 /* non-hotplug event; possibly handled by other handler */
447 status
= acpi_os_hotplug_execute(callback
, handle
);
448 if (ACPI_FAILURE(status
))
449 acpi_evaluate_hotplug_ost(handle
, type
,
450 ACPI_OST_SC_NON_SPECIFIC_FAILURE
,
455 * acpi_bus_hot_remove_device: hot-remove a device and its children
456 * @context: struct acpi_eject_event pointer (freed in this func)
458 * Hot-remove a device and its children. This function frees up the
459 * memory space passed by arg context, so that the caller may call
460 * this function asynchronously through acpi_os_hotplug_execute().
462 void acpi_bus_hot_remove_device(void *context
)
464 struct acpi_eject_event
*ej_event
= context
;
465 struct acpi_device
*device
= ej_event
->device
;
466 acpi_handle handle
= device
->handle
;
469 mutex_lock(&acpi_scan_lock
);
471 error
= acpi_scan_hot_remove(device
);
473 acpi_evaluate_hotplug_ost(handle
, ej_event
->event
,
474 ACPI_OST_SC_NON_SPECIFIC_FAILURE
,
477 mutex_unlock(&acpi_scan_lock
);
480 EXPORT_SYMBOL(acpi_bus_hot_remove_device
);
482 static ssize_t
real_power_state_show(struct device
*dev
,
483 struct device_attribute
*attr
, char *buf
)
485 struct acpi_device
*adev
= to_acpi_device(dev
);
489 ret
= acpi_device_get_power(adev
, &state
);
493 return sprintf(buf
, "%s\n", acpi_power_state_string(state
));
496 static DEVICE_ATTR(real_power_state
, 0444, real_power_state_show
, NULL
);
498 static ssize_t
power_state_show(struct device
*dev
,
499 struct device_attribute
*attr
, char *buf
)
501 struct acpi_device
*adev
= to_acpi_device(dev
);
503 return sprintf(buf
, "%s\n", acpi_power_state_string(adev
->power
.state
));
506 static DEVICE_ATTR(power_state
, 0444, power_state_show
, NULL
);
509 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
510 const char *buf
, size_t count
)
512 struct acpi_device
*acpi_device
= to_acpi_device(d
);
513 struct acpi_eject_event
*ej_event
;
514 acpi_object_type not_used
;
519 if (!count
|| buf
[0] != '1')
522 if ((!acpi_device
->handler
|| !acpi_device
->handler
->hotplug
.enabled
)
523 && !acpi_device
->driver
)
526 status
= acpi_get_type(acpi_device
->handle
, ¬_used
);
527 if (ACPI_FAILURE(status
) || !acpi_device
->flags
.ejectable
)
530 mutex_lock(&acpi_scan_lock
);
532 if (acpi_device
->flags
.eject_pending
) {
533 /* ACPI eject notification event. */
534 ost_source
= ACPI_NOTIFY_EJECT_REQUEST
;
535 acpi_device
->flags
.eject_pending
= 0;
537 /* Eject initiated by user space. */
538 ost_source
= ACPI_OST_EC_OSPM_EJECT
;
540 ej_event
= kmalloc(sizeof(*ej_event
), GFP_KERNEL
);
545 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ost_source
,
546 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
547 ej_event
->device
= acpi_device
;
548 ej_event
->event
= ost_source
;
549 get_device(&acpi_device
->dev
);
550 status
= acpi_os_hotplug_execute(acpi_bus_hot_remove_device
, ej_event
);
551 if (ACPI_FAILURE(status
)) {
552 put_device(&acpi_device
->dev
);
554 ret
= status
== AE_NO_MEMORY
? -ENOMEM
: -EAGAIN
;
560 mutex_unlock(&acpi_scan_lock
);
564 acpi_evaluate_hotplug_ost(acpi_device
->handle
, ost_source
,
565 ACPI_OST_SC_NON_SPECIFIC_FAILURE
, NULL
);
569 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
572 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
573 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
575 return sprintf(buf
, "%s\n", acpi_device_hid(acpi_dev
));
577 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
579 static ssize_t
acpi_device_uid_show(struct device
*dev
,
580 struct device_attribute
*attr
, char *buf
)
582 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
584 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.unique_id
);
586 static DEVICE_ATTR(uid
, 0444, acpi_device_uid_show
, NULL
);
588 static ssize_t
acpi_device_adr_show(struct device
*dev
,
589 struct device_attribute
*attr
, char *buf
)
591 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
593 return sprintf(buf
, "0x%08x\n",
594 (unsigned int)(acpi_dev
->pnp
.bus_address
));
596 static DEVICE_ATTR(adr
, 0444, acpi_device_adr_show
, NULL
);
599 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
600 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
601 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
604 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
608 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
613 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
615 /* sysfs file that shows description text from the ACPI _STR method */
616 static ssize_t
description_show(struct device
*dev
,
617 struct device_attribute
*attr
,
619 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
622 if (acpi_dev
->pnp
.str_obj
== NULL
)
626 * The _STR object contains a Unicode identifier for a device.
627 * We need to convert to utf-8 so it can be displayed.
629 result
= utf16s_to_utf8s(
630 (wchar_t *)acpi_dev
->pnp
.str_obj
->buffer
.pointer
,
631 acpi_dev
->pnp
.str_obj
->buffer
.length
,
632 UTF16_LITTLE_ENDIAN
, buf
,
635 buf
[result
++] = '\n';
639 static DEVICE_ATTR(description
, 0444, description_show
, NULL
);
642 acpi_device_sun_show(struct device
*dev
, struct device_attribute
*attr
,
644 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
646 return sprintf(buf
, "%lu\n", acpi_dev
->pnp
.sun
);
648 static DEVICE_ATTR(sun
, 0444, acpi_device_sun_show
, NULL
);
650 static int acpi_device_setup_files(struct acpi_device
*dev
)
652 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
654 unsigned long long sun
;
658 * Devices gotten from FADT don't have a "path" attribute
661 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
666 if (!list_empty(&dev
->pnp
.ids
)) {
667 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
671 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
677 * If device has _STR, 'description' file is created
679 if (acpi_has_method(dev
->handle
, "_STR")) {
680 status
= acpi_evaluate_object(dev
->handle
, "_STR",
682 if (ACPI_FAILURE(status
))
683 buffer
.pointer
= NULL
;
684 dev
->pnp
.str_obj
= buffer
.pointer
;
685 result
= device_create_file(&dev
->dev
, &dev_attr_description
);
690 if (dev
->pnp
.type
.bus_address
)
691 result
= device_create_file(&dev
->dev
, &dev_attr_adr
);
692 if (dev
->pnp
.unique_id
)
693 result
= device_create_file(&dev
->dev
, &dev_attr_uid
);
695 status
= acpi_evaluate_integer(dev
->handle
, "_SUN", NULL
, &sun
);
696 if (ACPI_SUCCESS(status
)) {
697 dev
->pnp
.sun
= (unsigned long)sun
;
698 result
= device_create_file(&dev
->dev
, &dev_attr_sun
);
702 dev
->pnp
.sun
= (unsigned long)-1;
706 * If device has _EJ0, 'eject' file is created that is used to trigger
707 * hot-removal function from userland.
709 if (acpi_has_method(dev
->handle
, "_EJ0")) {
710 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
715 if (dev
->flags
.power_manageable
) {
716 result
= device_create_file(&dev
->dev
, &dev_attr_power_state
);
720 if (dev
->power
.flags
.power_resources
)
721 result
= device_create_file(&dev
->dev
,
722 &dev_attr_real_power_state
);
729 static void acpi_device_remove_files(struct acpi_device
*dev
)
731 if (dev
->flags
.power_manageable
) {
732 device_remove_file(&dev
->dev
, &dev_attr_power_state
);
733 if (dev
->power
.flags
.power_resources
)
734 device_remove_file(&dev
->dev
,
735 &dev_attr_real_power_state
);
739 * If device has _STR, remove 'description' file
741 if (acpi_has_method(dev
->handle
, "_STR")) {
742 kfree(dev
->pnp
.str_obj
);
743 device_remove_file(&dev
->dev
, &dev_attr_description
);
746 * If device has _EJ0, remove 'eject' file.
748 if (acpi_has_method(dev
->handle
, "_EJ0"))
749 device_remove_file(&dev
->dev
, &dev_attr_eject
);
751 if (acpi_has_method(dev
->handle
, "_SUN"))
752 device_remove_file(&dev
->dev
, &dev_attr_sun
);
754 if (dev
->pnp
.unique_id
)
755 device_remove_file(&dev
->dev
, &dev_attr_uid
);
756 if (dev
->pnp
.type
.bus_address
)
757 device_remove_file(&dev
->dev
, &dev_attr_adr
);
758 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
759 device_remove_file(&dev
->dev
, &dev_attr_hid
);
761 device_remove_file(&dev
->dev
, &dev_attr_path
);
763 /* --------------------------------------------------------------------------
765 -------------------------------------------------------------------------- */
767 static const struct acpi_device_id
*__acpi_match_device(
768 struct acpi_device
*device
, const struct acpi_device_id
*ids
)
770 const struct acpi_device_id
*id
;
771 struct acpi_hardware_id
*hwid
;
774 * If the device is not present, it is unnecessary to load device
777 if (!device
->status
.present
)
780 for (id
= ids
; id
->id
[0]; id
++)
781 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
)
782 if (!strcmp((char *) id
->id
, hwid
->id
))
789 * acpi_match_device - Match a struct device against a given list of ACPI IDs
790 * @ids: Array of struct acpi_device_id object to match against.
791 * @dev: The device structure to match.
793 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
794 * object for that handle and use that object to match against a given list of
797 * Return a pointer to the first matching ID on success or %NULL on failure.
799 const struct acpi_device_id
*acpi_match_device(const struct acpi_device_id
*ids
,
800 const struct device
*dev
)
802 struct acpi_device
*adev
;
803 acpi_handle handle
= ACPI_HANDLE(dev
);
805 if (!ids
|| !handle
|| acpi_bus_get_device(handle
, &adev
))
808 return __acpi_match_device(adev
, ids
);
810 EXPORT_SYMBOL_GPL(acpi_match_device
);
812 int acpi_match_device_ids(struct acpi_device
*device
,
813 const struct acpi_device_id
*ids
)
815 return __acpi_match_device(device
, ids
) ? 0 : -ENOENT
;
817 EXPORT_SYMBOL(acpi_match_device_ids
);
819 static void acpi_free_power_resources_lists(struct acpi_device
*device
)
823 if (device
->wakeup
.flags
.valid
)
824 acpi_power_resources_list_free(&device
->wakeup
.resources
);
826 if (!device
->flags
.power_manageable
)
829 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++) {
830 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
831 acpi_power_resources_list_free(&ps
->resources
);
835 static void acpi_device_release(struct device
*dev
)
837 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
839 acpi_free_pnp_ids(&acpi_dev
->pnp
);
840 acpi_free_power_resources_lists(acpi_dev
);
844 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
846 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
847 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
849 return acpi_dev
->flags
.match_driver
850 && !acpi_match_device_ids(acpi_dev
, acpi_drv
->ids
);
853 static int acpi_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
855 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
858 if (list_empty(&acpi_dev
->pnp
.ids
))
861 if (add_uevent_var(env
, "MODALIAS="))
863 len
= create_modalias(acpi_dev
, &env
->buf
[env
->buflen
- 1],
864 sizeof(env
->buf
) - env
->buflen
);
865 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
871 static void acpi_device_notify(acpi_handle handle
, u32 event
, void *data
)
873 struct acpi_device
*device
= data
;
875 device
->driver
->ops
.notify(device
, event
);
878 static acpi_status
acpi_device_notify_fixed(void *data
)
880 struct acpi_device
*device
= data
;
882 /* Fixed hardware devices have no handles */
883 acpi_device_notify(NULL
, ACPI_FIXED_HARDWARE_EVENT
, device
);
887 static int acpi_device_install_notify_handler(struct acpi_device
*device
)
891 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
893 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
894 acpi_device_notify_fixed
,
896 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
898 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
899 acpi_device_notify_fixed
,
902 status
= acpi_install_notify_handler(device
->handle
,
907 if (ACPI_FAILURE(status
))
912 static void acpi_device_remove_notify_handler(struct acpi_device
*device
)
914 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
915 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
916 acpi_device_notify_fixed
);
917 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
918 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
919 acpi_device_notify_fixed
);
921 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
925 static int acpi_device_probe(struct device
*dev
)
927 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
928 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
931 if (acpi_dev
->handler
)
934 if (!acpi_drv
->ops
.add
)
937 ret
= acpi_drv
->ops
.add(acpi_dev
);
941 acpi_dev
->driver
= acpi_drv
;
942 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
943 "Driver [%s] successfully bound to device [%s]\n",
944 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
946 if (acpi_drv
->ops
.notify
) {
947 ret
= acpi_device_install_notify_handler(acpi_dev
);
949 if (acpi_drv
->ops
.remove
)
950 acpi_drv
->ops
.remove(acpi_dev
);
952 acpi_dev
->driver
= NULL
;
953 acpi_dev
->driver_data
= NULL
;
958 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found driver [%s] for device [%s]\n",
959 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
964 static int acpi_device_remove(struct device
* dev
)
966 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
967 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
970 if (acpi_drv
->ops
.notify
)
971 acpi_device_remove_notify_handler(acpi_dev
);
972 if (acpi_drv
->ops
.remove
)
973 acpi_drv
->ops
.remove(acpi_dev
);
975 acpi_dev
->driver
= NULL
;
976 acpi_dev
->driver_data
= NULL
;
982 struct bus_type acpi_bus_type
= {
984 .match
= acpi_bus_match
,
985 .probe
= acpi_device_probe
,
986 .remove
= acpi_device_remove
,
987 .uevent
= acpi_device_uevent
,
990 int acpi_device_add(struct acpi_device
*device
,
991 void (*release
)(struct device
*))
994 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
997 if (device
->handle
) {
1000 status
= acpi_attach_data(device
->handle
, acpi_bus_data_handler
,
1002 if (ACPI_FAILURE(status
)) {
1003 acpi_handle_err(device
->handle
,
1004 "Unable to attach device data\n");
1012 * Link this device to its parent and siblings.
1014 INIT_LIST_HEAD(&device
->children
);
1015 INIT_LIST_HEAD(&device
->node
);
1016 INIT_LIST_HEAD(&device
->wakeup_list
);
1017 INIT_LIST_HEAD(&device
->physical_node_list
);
1018 mutex_init(&device
->physical_node_lock
);
1019 INIT_LIST_HEAD(&device
->power_dependent
);
1021 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
1023 pr_err(PREFIX
"Memory allocation error\n");
1028 mutex_lock(&acpi_device_lock
);
1030 * Find suitable bus_id and instance number in acpi_bus_id_list
1031 * If failed, create one and link it into acpi_bus_id_list
1033 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
1034 if (!strcmp(acpi_device_bus_id
->bus_id
,
1035 acpi_device_hid(device
))) {
1036 acpi_device_bus_id
->instance_no
++;
1043 acpi_device_bus_id
= new_bus_id
;
1044 strcpy(acpi_device_bus_id
->bus_id
, acpi_device_hid(device
));
1045 acpi_device_bus_id
->instance_no
= 0;
1046 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
1048 dev_set_name(&device
->dev
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
1051 list_add_tail(&device
->node
, &device
->parent
->children
);
1053 if (device
->wakeup
.flags
.valid
)
1054 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
1055 mutex_unlock(&acpi_device_lock
);
1058 device
->dev
.parent
= &device
->parent
->dev
;
1059 device
->dev
.bus
= &acpi_bus_type
;
1060 device
->dev
.release
= release
;
1061 result
= device_add(&device
->dev
);
1063 dev_err(&device
->dev
, "Error registering device\n");
1067 result
= acpi_device_setup_files(device
);
1069 printk(KERN_ERR PREFIX
"Error creating sysfs interface for device %s\n",
1070 dev_name(&device
->dev
));
1075 mutex_lock(&acpi_device_lock
);
1077 list_del(&device
->node
);
1078 list_del(&device
->wakeup_list
);
1079 mutex_unlock(&acpi_device_lock
);
1082 acpi_detach_data(device
->handle
, acpi_bus_data_handler
);
1086 static void acpi_device_unregister(struct acpi_device
*device
)
1088 mutex_lock(&acpi_device_lock
);
1090 list_del(&device
->node
);
1092 list_del(&device
->wakeup_list
);
1093 mutex_unlock(&acpi_device_lock
);
1095 acpi_detach_data(device
->handle
, acpi_bus_data_handler
);
1097 acpi_power_add_remove_device(device
, false);
1098 acpi_device_remove_files(device
);
1100 device
->remove(device
);
1102 device_del(&device
->dev
);
1104 * Transition the device to D3cold to drop the reference counts of all
1105 * power resources the device depends on and turn off the ones that have
1106 * no more references.
1108 acpi_device_set_power(device
, ACPI_STATE_D3_COLD
);
1109 device
->handle
= NULL
;
1110 put_device(&device
->dev
);
1113 /* --------------------------------------------------------------------------
1115 -------------------------------------------------------------------------- */
1117 * acpi_bus_register_driver - register a driver with the ACPI bus
1118 * @driver: driver being registered
1120 * Registers a driver with the ACPI bus. Searches the namespace for all
1121 * devices that match the driver's criteria and binds. Returns zero for
1122 * success or a negative error status for failure.
1124 int acpi_bus_register_driver(struct acpi_driver
*driver
)
1130 driver
->drv
.name
= driver
->name
;
1131 driver
->drv
.bus
= &acpi_bus_type
;
1132 driver
->drv
.owner
= driver
->owner
;
1134 ret
= driver_register(&driver
->drv
);
1138 EXPORT_SYMBOL(acpi_bus_register_driver
);
1141 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1142 * @driver: driver to unregister
1144 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1145 * devices that match the driver's criteria and unbinds.
1147 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
1149 driver_unregister(&driver
->drv
);
1152 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
1154 /* --------------------------------------------------------------------------
1156 -------------------------------------------------------------------------- */
1157 static struct acpi_device
*acpi_bus_get_parent(acpi_handle handle
)
1159 struct acpi_device
*device
= NULL
;
1163 * Fixed hardware devices do not appear in the namespace and do not
1164 * have handles, but we fabricate acpi_devices for them, so we have
1165 * to deal with them specially.
1171 status
= acpi_get_parent(handle
, &handle
);
1172 if (ACPI_FAILURE(status
))
1173 return status
== AE_NULL_ENTRY
? NULL
: acpi_root
;
1174 } while (acpi_bus_get_device(handle
, &device
));
1179 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
1183 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1184 union acpi_object
*obj
;
1186 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
1187 if (ACPI_FAILURE(status
))
1190 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
1191 if (ACPI_SUCCESS(status
)) {
1192 obj
= buffer
.pointer
;
1193 status
= acpi_get_handle(ACPI_ROOT_OBJECT
, obj
->string
.pointer
,
1195 kfree(buffer
.pointer
);
1199 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
1201 void acpi_bus_data_handler(acpi_handle handle
, void *context
)
1209 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle
,
1210 struct acpi_device_wakeup
*wakeup
)
1212 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1213 union acpi_object
*package
= NULL
;
1214 union acpi_object
*element
= NULL
;
1221 INIT_LIST_HEAD(&wakeup
->resources
);
1224 status
= acpi_evaluate_object(handle
, "_PRW", NULL
, &buffer
);
1225 if (ACPI_FAILURE(status
)) {
1226 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
1230 package
= (union acpi_object
*)buffer
.pointer
;
1232 if (!package
|| package
->package
.count
< 2)
1235 element
= &(package
->package
.elements
[0]);
1239 if (element
->type
== ACPI_TYPE_PACKAGE
) {
1240 if ((element
->package
.count
< 2) ||
1241 (element
->package
.elements
[0].type
!=
1242 ACPI_TYPE_LOCAL_REFERENCE
)
1243 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
1246 wakeup
->gpe_device
=
1247 element
->package
.elements
[0].reference
.handle
;
1248 wakeup
->gpe_number
=
1249 (u32
) element
->package
.elements
[1].integer
.value
;
1250 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
1251 wakeup
->gpe_device
= NULL
;
1252 wakeup
->gpe_number
= element
->integer
.value
;
1257 element
= &(package
->package
.elements
[1]);
1258 if (element
->type
!= ACPI_TYPE_INTEGER
)
1261 wakeup
->sleep_state
= element
->integer
.value
;
1263 err
= acpi_extract_power_resources(package
, 2, &wakeup
->resources
);
1267 if (!list_empty(&wakeup
->resources
)) {
1270 err
= acpi_power_wakeup_list_init(&wakeup
->resources
,
1273 acpi_handle_warn(handle
, "Retrieving current states "
1274 "of wakeup power resources failed\n");
1275 acpi_power_resources_list_free(&wakeup
->resources
);
1278 if (sleep_state
< wakeup
->sleep_state
) {
1279 acpi_handle_warn(handle
, "Overriding _PRW sleep state "
1280 "(S%d) by S%d from power resources\n",
1281 (int)wakeup
->sleep_state
, sleep_state
);
1282 wakeup
->sleep_state
= sleep_state
;
1285 acpi_setup_gpe_for_wake(handle
, wakeup
->gpe_device
, wakeup
->gpe_number
);
1288 kfree(buffer
.pointer
);
1292 static void acpi_bus_set_run_wake_flags(struct acpi_device
*device
)
1294 struct acpi_device_id button_device_ids
[] = {
1301 acpi_event_status event_status
;
1303 device
->wakeup
.flags
.notifier_present
= 0;
1305 /* Power button, Lid switch always enable wakeup */
1306 if (!acpi_match_device_ids(device
, button_device_ids
)) {
1307 device
->wakeup
.flags
.run_wake
= 1;
1308 if (!acpi_match_device_ids(device
, &button_device_ids
[1])) {
1309 /* Do not use Lid/sleep button for S5 wakeup */
1310 if (device
->wakeup
.sleep_state
== ACPI_STATE_S5
)
1311 device
->wakeup
.sleep_state
= ACPI_STATE_S4
;
1313 device_set_wakeup_capable(&device
->dev
, true);
1317 status
= acpi_get_gpe_status(device
->wakeup
.gpe_device
,
1318 device
->wakeup
.gpe_number
,
1320 if (status
== AE_OK
)
1321 device
->wakeup
.flags
.run_wake
=
1322 !!(event_status
& ACPI_EVENT_FLAG_HANDLE
);
1325 static void acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
1329 /* Presence of _PRW indicates wake capable */
1330 if (!acpi_has_method(device
->handle
, "_PRW"))
1333 err
= acpi_bus_extract_wakeup_device_power_package(device
->handle
,
1336 dev_err(&device
->dev
, "_PRW evaluation error: %d\n", err
);
1340 device
->wakeup
.flags
.valid
= 1;
1341 device
->wakeup
.prepare_count
= 0;
1342 acpi_bus_set_run_wake_flags(device
);
1343 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1344 * system for the ACPI device with the _PRW object.
1345 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1346 * So it is necessary to call _DSW object first. Only when it is not
1347 * present will the _PSW object used.
1349 err
= acpi_device_sleep_wake(device
, 0, 0, 0);
1351 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1352 "error in _DSW or _PSW evaluation\n"));
1355 static void acpi_bus_init_power_state(struct acpi_device
*device
, int state
)
1357 struct acpi_device_power_state
*ps
= &device
->power
.states
[state
];
1358 char pathname
[5] = { '_', 'P', 'R', '0' + state
, '\0' };
1359 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1362 INIT_LIST_HEAD(&ps
->resources
);
1364 /* Evaluate "_PRx" to get referenced power resources */
1365 status
= acpi_evaluate_object(device
->handle
, pathname
, NULL
, &buffer
);
1366 if (ACPI_SUCCESS(status
)) {
1367 union acpi_object
*package
= buffer
.pointer
;
1369 if (buffer
.length
&& package
1370 && package
->type
== ACPI_TYPE_PACKAGE
1371 && package
->package
.count
) {
1372 int err
= acpi_extract_power_resources(package
, 0,
1375 device
->power
.flags
.power_resources
= 1;
1377 ACPI_FREE(buffer
.pointer
);
1380 /* Evaluate "_PSx" to see if we can do explicit sets */
1382 if (acpi_has_method(device
->handle
, pathname
))
1383 ps
->flags
.explicit_set
= 1;
1386 * State is valid if there are means to put the device into it.
1387 * D3hot is only valid if _PR3 present.
1389 if (!list_empty(&ps
->resources
)
1390 || (ps
->flags
.explicit_set
&& state
< ACPI_STATE_D3_HOT
)) {
1391 ps
->flags
.valid
= 1;
1392 ps
->flags
.os_accessible
= 1;
1395 ps
->power
= -1; /* Unknown - driver assigned */
1396 ps
->latency
= -1; /* Unknown - driver assigned */
1399 static void acpi_bus_get_power_flags(struct acpi_device
*device
)
1403 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1404 if (!acpi_has_method(device
->handle
, "_PS0") &&
1405 !acpi_has_method(device
->handle
, "_PR0"))
1408 device
->flags
.power_manageable
= 1;
1411 * Power Management Flags
1413 if (acpi_has_method(device
->handle
, "_PSC"))
1414 device
->power
.flags
.explicit_get
= 1;
1415 if (acpi_has_method(device
->handle
, "_IRC"))
1416 device
->power
.flags
.inrush_current
= 1;
1419 * Enumerate supported power management states
1421 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++)
1422 acpi_bus_init_power_state(device
, i
);
1424 INIT_LIST_HEAD(&device
->power
.states
[ACPI_STATE_D3_COLD
].resources
);
1426 /* Set defaults for D0 and D3 states (always valid) */
1427 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
1428 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
1429 device
->power
.states
[ACPI_STATE_D3
].flags
.valid
= 1;
1430 device
->power
.states
[ACPI_STATE_D3
].power
= 0;
1432 /* Set D3cold's explicit_set flag if _PS3 exists. */
1433 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
)
1434 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.explicit_set
= 1;
1436 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1437 if (device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.explicit_set
||
1438 device
->power
.flags
.power_resources
)
1439 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.os_accessible
= 1;
1441 if (acpi_bus_init_power(device
)) {
1442 acpi_free_power_resources_lists(device
);
1443 device
->flags
.power_manageable
= 0;
1447 static void acpi_bus_get_flags(struct acpi_device
*device
)
1449 /* Presence of _STA indicates 'dynamic_status' */
1450 if (acpi_has_method(device
->handle
, "_STA"))
1451 device
->flags
.dynamic_status
= 1;
1453 /* Presence of _RMV indicates 'removable' */
1454 if (acpi_has_method(device
->handle
, "_RMV"))
1455 device
->flags
.removable
= 1;
1457 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1458 if (acpi_has_method(device
->handle
, "_EJD") ||
1459 acpi_has_method(device
->handle
, "_EJ0"))
1460 device
->flags
.ejectable
= 1;
1463 static void acpi_device_get_busid(struct acpi_device
*device
)
1465 char bus_id
[5] = { '?', 0 };
1466 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
1472 * The device's Bus ID is simply the object name.
1473 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1475 if (ACPI_IS_ROOT_DEVICE(device
)) {
1476 strcpy(device
->pnp
.bus_id
, "ACPI");
1480 switch (device
->device_type
) {
1481 case ACPI_BUS_TYPE_POWER_BUTTON
:
1482 strcpy(device
->pnp
.bus_id
, "PWRF");
1484 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1485 strcpy(device
->pnp
.bus_id
, "SLPF");
1488 acpi_get_name(device
->handle
, ACPI_SINGLE_NAME
, &buffer
);
1489 /* Clean up trailing underscores (if any) */
1490 for (i
= 3; i
> 1; i
--) {
1491 if (bus_id
[i
] == '_')
1496 strcpy(device
->pnp
.bus_id
, bus_id
);
1502 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1504 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1505 * then we can safely call it an ejectable drive bay
1507 static int acpi_bay_match(acpi_handle handle
)
1509 acpi_handle phandle
;
1511 if (!acpi_has_method(handle
, "_EJ0"))
1514 if (acpi_has_method(handle
, "_GTF") ||
1515 acpi_has_method(handle
, "_GTM") ||
1516 acpi_has_method(handle
, "_STM") ||
1517 acpi_has_method(handle
, "_SDD"))
1520 if (acpi_get_parent(handle
, &phandle
))
1523 if (acpi_has_method(phandle
, "_GTF") ||
1524 acpi_has_method(phandle
, "_GTM") ||
1525 acpi_has_method(phandle
, "_STM") ||
1526 acpi_has_method(phandle
, "_SDD"))
1533 * acpi_dock_match - see if an acpi object has a _DCK method
1535 static int acpi_dock_match(acpi_handle handle
)
1538 return acpi_get_handle(handle
, "_DCK", &tmp
);
1541 const char *acpi_device_hid(struct acpi_device
*device
)
1543 struct acpi_hardware_id
*hid
;
1545 if (list_empty(&device
->pnp
.ids
))
1548 hid
= list_first_entry(&device
->pnp
.ids
, struct acpi_hardware_id
, list
);
1551 EXPORT_SYMBOL(acpi_device_hid
);
1553 static void acpi_add_id(struct acpi_device_pnp
*pnp
, const char *dev_id
)
1555 struct acpi_hardware_id
*id
;
1557 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
1561 id
->id
= kstrdup(dev_id
, GFP_KERNEL
);
1567 list_add_tail(&id
->list
, &pnp
->ids
);
1568 pnp
->type
.hardware_id
= 1;
1572 * Old IBM workstations have a DSDT bug wherein the SMBus object
1573 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1574 * prefix. Work around this.
1576 static int acpi_ibm_smbus_match(acpi_handle handle
)
1578 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1581 if (!dmi_name_in_vendors("IBM"))
1584 /* Look for SMBS object */
1585 result
= acpi_get_name(handle
, ACPI_SINGLE_NAME
, &path
);
1589 if (strcmp("SMBS", path
.pointer
)) {
1594 /* Does it have the necessary (but misnamed) methods? */
1596 if (acpi_has_method(handle
, "SBI") &&
1597 acpi_has_method(handle
, "SBR") &&
1598 acpi_has_method(handle
, "SBW"))
1601 kfree(path
.pointer
);
1605 static void acpi_set_pnp_ids(acpi_handle handle
, struct acpi_device_pnp
*pnp
,
1609 struct acpi_device_info
*info
;
1610 struct acpi_pnp_device_id_list
*cid_list
;
1613 switch (device_type
) {
1614 case ACPI_BUS_TYPE_DEVICE
:
1615 if (handle
== ACPI_ROOT_OBJECT
) {
1616 acpi_add_id(pnp
, ACPI_SYSTEM_HID
);
1620 status
= acpi_get_object_info(handle
, &info
);
1621 if (ACPI_FAILURE(status
)) {
1622 pr_err(PREFIX
"%s: Error reading device info\n",
1627 if (info
->valid
& ACPI_VALID_HID
)
1628 acpi_add_id(pnp
, info
->hardware_id
.string
);
1629 if (info
->valid
& ACPI_VALID_CID
) {
1630 cid_list
= &info
->compatible_id_list
;
1631 for (i
= 0; i
< cid_list
->count
; i
++)
1632 acpi_add_id(pnp
, cid_list
->ids
[i
].string
);
1634 if (info
->valid
& ACPI_VALID_ADR
) {
1635 pnp
->bus_address
= info
->address
;
1636 pnp
->type
.bus_address
= 1;
1638 if (info
->valid
& ACPI_VALID_UID
)
1639 pnp
->unique_id
= kstrdup(info
->unique_id
.string
,
1645 * Some devices don't reliably have _HIDs & _CIDs, so add
1646 * synthetic HIDs to make sure drivers can find them.
1648 if (acpi_is_video_device(handle
))
1649 acpi_add_id(pnp
, ACPI_VIDEO_HID
);
1650 else if (ACPI_SUCCESS(acpi_bay_match(handle
)))
1651 acpi_add_id(pnp
, ACPI_BAY_HID
);
1652 else if (ACPI_SUCCESS(acpi_dock_match(handle
)))
1653 acpi_add_id(pnp
, ACPI_DOCK_HID
);
1654 else if (!acpi_ibm_smbus_match(handle
))
1655 acpi_add_id(pnp
, ACPI_SMBUS_IBM_HID
);
1656 else if (list_empty(&pnp
->ids
) && handle
== ACPI_ROOT_OBJECT
) {
1657 acpi_add_id(pnp
, ACPI_BUS_HID
); /* \_SB, LNXSYBUS */
1658 strcpy(pnp
->device_name
, ACPI_BUS_DEVICE_NAME
);
1659 strcpy(pnp
->device_class
, ACPI_BUS_CLASS
);
1663 case ACPI_BUS_TYPE_POWER
:
1664 acpi_add_id(pnp
, ACPI_POWER_HID
);
1666 case ACPI_BUS_TYPE_PROCESSOR
:
1667 acpi_add_id(pnp
, ACPI_PROCESSOR_OBJECT_HID
);
1669 case ACPI_BUS_TYPE_THERMAL
:
1670 acpi_add_id(pnp
, ACPI_THERMAL_HID
);
1672 case ACPI_BUS_TYPE_POWER_BUTTON
:
1673 acpi_add_id(pnp
, ACPI_BUTTON_HID_POWERF
);
1675 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1676 acpi_add_id(pnp
, ACPI_BUTTON_HID_SLEEPF
);
1681 void acpi_free_pnp_ids(struct acpi_device_pnp
*pnp
)
1683 struct acpi_hardware_id
*id
, *tmp
;
1685 list_for_each_entry_safe(id
, tmp
, &pnp
->ids
, list
) {
1689 kfree(pnp
->unique_id
);
1692 void acpi_init_device_object(struct acpi_device
*device
, acpi_handle handle
,
1693 int type
, unsigned long long sta
)
1695 INIT_LIST_HEAD(&device
->pnp
.ids
);
1696 device
->device_type
= type
;
1697 device
->handle
= handle
;
1698 device
->parent
= acpi_bus_get_parent(handle
);
1699 STRUCT_TO_INT(device
->status
) = sta
;
1700 acpi_device_get_busid(device
);
1701 acpi_set_pnp_ids(handle
, &device
->pnp
, type
);
1702 acpi_bus_get_flags(device
);
1703 device
->flags
.match_driver
= false;
1704 device_initialize(&device
->dev
);
1705 dev_set_uevent_suppress(&device
->dev
, true);
1708 void acpi_device_add_finalize(struct acpi_device
*device
)
1710 device
->flags
.match_driver
= true;
1711 dev_set_uevent_suppress(&device
->dev
, false);
1712 kobject_uevent(&device
->dev
.kobj
, KOBJ_ADD
);
1715 static int acpi_add_single_object(struct acpi_device
**child
,
1716 acpi_handle handle
, int type
,
1717 unsigned long long sta
)
1720 struct acpi_device
*device
;
1721 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1723 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
1725 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1729 acpi_init_device_object(device
, handle
, type
, sta
);
1730 acpi_bus_get_power_flags(device
);
1731 acpi_bus_get_wakeup_device_flags(device
);
1733 result
= acpi_device_add(device
, acpi_device_release
);
1735 acpi_device_release(&device
->dev
);
1739 acpi_power_add_remove_device(device
, true);
1740 acpi_device_add_finalize(device
);
1741 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1742 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Added %s [%s] parent %s\n",
1743 dev_name(&device
->dev
), (char *) buffer
.pointer
,
1744 device
->parent
? dev_name(&device
->parent
->dev
) : "(null)"));
1745 kfree(buffer
.pointer
);
1750 static int acpi_bus_type_and_status(acpi_handle handle
, int *type
,
1751 unsigned long long *sta
)
1754 acpi_object_type acpi_type
;
1756 status
= acpi_get_type(handle
, &acpi_type
);
1757 if (ACPI_FAILURE(status
))
1760 switch (acpi_type
) {
1761 case ACPI_TYPE_ANY
: /* for ACPI_ROOT_OBJECT */
1762 case ACPI_TYPE_DEVICE
:
1763 *type
= ACPI_BUS_TYPE_DEVICE
;
1764 status
= acpi_bus_get_status_handle(handle
, sta
);
1765 if (ACPI_FAILURE(status
))
1768 case ACPI_TYPE_PROCESSOR
:
1769 *type
= ACPI_BUS_TYPE_PROCESSOR
;
1770 status
= acpi_bus_get_status_handle(handle
, sta
);
1771 if (ACPI_FAILURE(status
))
1774 case ACPI_TYPE_THERMAL
:
1775 *type
= ACPI_BUS_TYPE_THERMAL
;
1776 *sta
= ACPI_STA_DEFAULT
;
1778 case ACPI_TYPE_POWER
:
1779 *type
= ACPI_BUS_TYPE_POWER
;
1780 *sta
= ACPI_STA_DEFAULT
;
1789 static bool acpi_scan_handler_matching(struct acpi_scan_handler
*handler
,
1791 const struct acpi_device_id
**matchid
)
1793 const struct acpi_device_id
*devid
;
1795 for (devid
= handler
->ids
; devid
->id
[0]; devid
++)
1796 if (!strcmp((char *)devid
->id
, idstr
)) {
1806 static struct acpi_scan_handler
*acpi_scan_match_handler(char *idstr
,
1807 const struct acpi_device_id
**matchid
)
1809 struct acpi_scan_handler
*handler
;
1811 list_for_each_entry(handler
, &acpi_scan_handlers_list
, list_node
)
1812 if (acpi_scan_handler_matching(handler
, idstr
, matchid
))
1818 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile
*hotplug
, bool val
)
1820 if (!!hotplug
->enabled
== !!val
)
1823 mutex_lock(&acpi_scan_lock
);
1825 hotplug
->enabled
= val
;
1827 mutex_unlock(&acpi_scan_lock
);
1830 static void acpi_scan_init_hotplug(acpi_handle handle
, int type
)
1832 struct acpi_device_pnp pnp
= {};
1833 struct acpi_hardware_id
*hwid
;
1834 struct acpi_scan_handler
*handler
;
1836 INIT_LIST_HEAD(&pnp
.ids
);
1837 acpi_set_pnp_ids(handle
, &pnp
, type
);
1839 if (!pnp
.type
.hardware_id
)
1843 * This relies on the fact that acpi_install_notify_handler() will not
1844 * install the same notify handler routine twice for the same handle.
1846 list_for_each_entry(hwid
, &pnp
.ids
, list
) {
1847 handler
= acpi_scan_match_handler(hwid
->id
, NULL
);
1849 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
1850 acpi_hotplug_notify_cb
, handler
);
1856 acpi_free_pnp_ids(&pnp
);
1859 static acpi_status
acpi_bus_check_add(acpi_handle handle
, u32 lvl_not_used
,
1860 void *not_used
, void **return_value
)
1862 struct acpi_device
*device
= NULL
;
1864 unsigned long long sta
;
1867 acpi_bus_get_device(handle
, &device
);
1871 result
= acpi_bus_type_and_status(handle
, &type
, &sta
);
1875 if (type
== ACPI_BUS_TYPE_POWER
) {
1876 acpi_add_power_resource(handle
);
1880 acpi_scan_init_hotplug(handle
, type
);
1882 if (!(sta
& ACPI_STA_DEVICE_PRESENT
) &&
1883 !(sta
& ACPI_STA_DEVICE_FUNCTIONING
)) {
1884 struct acpi_device_wakeup wakeup
;
1886 if (acpi_has_method(handle
, "_PRW")) {
1887 acpi_bus_extract_wakeup_device_power_package(handle
,
1889 acpi_power_resources_list_free(&wakeup
.resources
);
1891 return AE_CTRL_DEPTH
;
1894 acpi_add_single_object(&device
, handle
, type
, sta
);
1896 return AE_CTRL_DEPTH
;
1900 *return_value
= device
;
1905 static int acpi_scan_attach_handler(struct acpi_device
*device
)
1907 struct acpi_hardware_id
*hwid
;
1910 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
) {
1911 const struct acpi_device_id
*devid
;
1912 struct acpi_scan_handler
*handler
;
1914 handler
= acpi_scan_match_handler(hwid
->id
, &devid
);
1916 ret
= handler
->attach(device
, devid
);
1918 device
->handler
= handler
;
1920 } else if (ret
< 0) {
1928 static acpi_status
acpi_bus_device_attach(acpi_handle handle
, u32 lvl_not_used
,
1929 void *not_used
, void **ret_not_used
)
1931 struct acpi_device
*device
;
1932 unsigned long long sta_not_used
;
1936 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1937 * namespace walks prematurely.
1939 if (acpi_bus_type_and_status(handle
, &ret
, &sta_not_used
))
1942 if (acpi_bus_get_device(handle
, &device
))
1943 return AE_CTRL_DEPTH
;
1945 ret
= acpi_scan_attach_handler(device
);
1947 return ret
> 0 ? AE_OK
: AE_CTRL_DEPTH
;
1949 ret
= device_attach(&device
->dev
);
1950 return ret
>= 0 ? AE_OK
: AE_CTRL_DEPTH
;
1954 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1955 * @handle: Root of the namespace scope to scan.
1957 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1960 * If no devices were found, -ENODEV is returned, but it does not mean that
1961 * there has been a real error. There just have been no suitable ACPI objects
1962 * in the table trunk from which the kernel could create a device and add an
1963 * appropriate driver.
1965 * Must be called under acpi_scan_lock.
1967 int acpi_bus_scan(acpi_handle handle
)
1969 void *device
= NULL
;
1972 if (ACPI_SUCCESS(acpi_bus_check_add(handle
, 0, NULL
, &device
)))
1973 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
1974 acpi_bus_check_add
, NULL
, NULL
, &device
);
1978 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle
, 0, NULL
, NULL
)))
1979 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
1980 acpi_bus_device_attach
, NULL
, NULL
, NULL
);
1984 EXPORT_SYMBOL(acpi_bus_scan
);
1986 static acpi_status
acpi_bus_device_detach(acpi_handle handle
, u32 lvl_not_used
,
1987 void *not_used
, void **ret_not_used
)
1989 struct acpi_device
*device
= NULL
;
1991 if (!acpi_bus_get_device(handle
, &device
)) {
1992 struct acpi_scan_handler
*dev_handler
= device
->handler
;
1995 if (dev_handler
->detach
)
1996 dev_handler
->detach(device
);
1998 device
->handler
= NULL
;
2000 device_release_driver(&device
->dev
);
2006 static acpi_status
acpi_bus_remove(acpi_handle handle
, u32 lvl_not_used
,
2007 void *not_used
, void **ret_not_used
)
2009 struct acpi_device
*device
= NULL
;
2011 if (!acpi_bus_get_device(handle
, &device
))
2012 acpi_device_unregister(device
);
2018 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2019 * @start: Root of the ACPI device nodes subtree to remove.
2021 * Must be called under acpi_scan_lock.
2023 void acpi_bus_trim(struct acpi_device
*start
)
2026 * Execute acpi_bus_device_detach() as a post-order callback to detach
2027 * all ACPI drivers from the device nodes being removed.
2029 acpi_walk_namespace(ACPI_TYPE_ANY
, start
->handle
, ACPI_UINT32_MAX
, NULL
,
2030 acpi_bus_device_detach
, NULL
, NULL
);
2031 acpi_bus_device_detach(start
->handle
, 0, NULL
, NULL
);
2033 * Execute acpi_bus_remove() as a post-order callback to remove device
2034 * nodes in the given namespace scope.
2036 acpi_walk_namespace(ACPI_TYPE_ANY
, start
->handle
, ACPI_UINT32_MAX
, NULL
,
2037 acpi_bus_remove
, NULL
, NULL
);
2038 acpi_bus_remove(start
->handle
, 0, NULL
, NULL
);
2040 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
2042 static int acpi_bus_scan_fixed(void)
2047 * Enumerate all fixed-feature devices.
2049 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
)) {
2050 struct acpi_device
*device
= NULL
;
2052 result
= acpi_add_single_object(&device
, NULL
,
2053 ACPI_BUS_TYPE_POWER_BUTTON
,
2058 result
= device_attach(&device
->dev
);
2062 device_init_wakeup(&device
->dev
, true);
2065 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
)) {
2066 struct acpi_device
*device
= NULL
;
2068 result
= acpi_add_single_object(&device
, NULL
,
2069 ACPI_BUS_TYPE_SLEEP_BUTTON
,
2074 result
= device_attach(&device
->dev
);
2077 return result
< 0 ? result
: 0;
2080 int __init
acpi_scan_init(void)
2084 result
= bus_register(&acpi_bus_type
);
2086 /* We don't want to quit even if we failed to add suspend/resume */
2087 printk(KERN_ERR PREFIX
"Could not register bus type\n");
2090 acpi_pci_root_init();
2091 acpi_pci_link_init();
2092 acpi_processor_init();
2093 acpi_platform_init();
2095 acpi_cmos_rtc_init();
2096 acpi_container_init();
2097 acpi_memory_hotplug_init();
2100 mutex_lock(&acpi_scan_lock
);
2102 * Enumerate devices in the ACPI namespace.
2104 result
= acpi_bus_scan(ACPI_ROOT_OBJECT
);
2108 result
= acpi_bus_get_device(ACPI_ROOT_OBJECT
, &acpi_root
);
2112 result
= acpi_bus_scan_fixed();
2114 acpi_device_unregister(acpi_root
);
2118 acpi_update_all_gpes();
2120 acpi_pci_root_hp_init();
2123 mutex_unlock(&acpi_scan_lock
);