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/kernel.h>
8 #include <linux/acpi.h>
9 #include <linux/signal.h>
10 #include <linux/kthread.h>
12 #include <acpi/acpi_drivers.h>
16 #define _COMPONENT ACPI_BUS_COMPONENT
17 ACPI_MODULE_NAME("scan");
18 #define STRUCT_TO_INT(s) (*((int*)&s))
19 extern struct acpi_device
*acpi_root
;
21 #define ACPI_BUS_CLASS "system_bus"
22 #define ACPI_BUS_HID "LNXSYBUS"
23 #define ACPI_BUS_DEVICE_NAME "System Bus"
25 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
27 static LIST_HEAD(acpi_device_list
);
28 static LIST_HEAD(acpi_bus_id_list
);
29 DEFINE_MUTEX(acpi_device_lock
);
30 LIST_HEAD(acpi_wakeup_device_list
);
32 struct acpi_device_bus_id
{
34 unsigned int instance_no
;
35 struct list_head node
;
39 * Creates hid/cid(s) string needed for modalias and uevent
40 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41 * char *modalias: "acpi:IBM0001:ACPI0001"
43 static int create_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
48 struct acpi_hardware_id
*id
;
50 len
= snprintf(modalias
, size
, "acpi:");
53 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
) {
54 count
= snprintf(&modalias
[len
], size
, "%s:", id
->id
);
55 if (count
< 0 || count
>= size
)
66 acpi_device_modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
67 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
70 /* Device has no HID and no CID or string is >1024 */
71 len
= create_modalias(acpi_dev
, buf
, 1024);
77 static DEVICE_ATTR(modalias
, 0444, acpi_device_modalias_show
, NULL
);
79 static void acpi_bus_hot_remove_device(void *context
)
81 struct acpi_device
*device
;
82 acpi_handle handle
= context
;
83 struct acpi_object_list arg_list
;
84 union acpi_object arg
;
85 acpi_status status
= AE_OK
;
87 if (acpi_bus_get_device(handle
, &device
))
93 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
94 "Hot-removing device %s...\n", dev_name(&device
->dev
)));
96 if (acpi_bus_trim(device
, 1)) {
97 printk(KERN_ERR PREFIX
98 "Removing device failed\n");
102 /* power off device */
103 status
= acpi_evaluate_object(handle
, "_PS3", NULL
, NULL
);
104 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
)
105 printk(KERN_WARNING PREFIX
106 "Power-off device failed\n");
108 if (device
->flags
.lockable
) {
110 arg_list
.pointer
= &arg
;
111 arg
.type
= ACPI_TYPE_INTEGER
;
112 arg
.integer
.value
= 0;
113 acpi_evaluate_object(handle
, "_LCK", &arg_list
, NULL
);
117 arg_list
.pointer
= &arg
;
118 arg
.type
= ACPI_TYPE_INTEGER
;
119 arg
.integer
.value
= 1;
124 status
= acpi_evaluate_object(handle
, "_EJ0", &arg_list
, NULL
);
125 if (ACPI_FAILURE(status
))
126 printk(KERN_WARNING PREFIX
127 "Eject device failed\n");
133 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
134 const char *buf
, size_t count
)
138 acpi_object_type type
= 0;
139 struct acpi_device
*acpi_device
= to_acpi_device(d
);
141 if ((!count
) || (buf
[0] != '1')) {
145 if (acpi_device
->driver
== NULL
) {
150 status
= acpi_get_type(acpi_device
->handle
, &type
);
151 if (ACPI_FAILURE(status
) || (!acpi_device
->flags
.ejectable
)) {
156 acpi_os_hotplug_execute(acpi_bus_hot_remove_device
, acpi_device
->handle
);
161 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
164 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
165 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
167 return sprintf(buf
, "%s\n", acpi_device_hid(acpi_dev
));
169 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
172 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
173 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
174 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
177 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
181 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
186 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
188 static int acpi_device_setup_files(struct acpi_device
*dev
)
195 * Devices gotten from FADT don't have a "path" attribute
198 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
203 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
207 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
212 * If device has _EJ0, 'eject' file is created that is used to trigger
213 * hot-removal function from userland.
215 status
= acpi_get_handle(dev
->handle
, "_EJ0", &temp
);
216 if (ACPI_SUCCESS(status
))
217 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
222 static void acpi_device_remove_files(struct acpi_device
*dev
)
228 * If device has _EJ0, 'eject' file is created that is used to trigger
229 * hot-removal function from userland.
231 status
= acpi_get_handle(dev
->handle
, "_EJ0", &temp
);
232 if (ACPI_SUCCESS(status
))
233 device_remove_file(&dev
->dev
, &dev_attr_eject
);
235 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
236 device_remove_file(&dev
->dev
, &dev_attr_hid
);
238 device_remove_file(&dev
->dev
, &dev_attr_path
);
240 /* --------------------------------------------------------------------------
242 -------------------------------------------------------------------------- */
244 int acpi_match_device_ids(struct acpi_device
*device
,
245 const struct acpi_device_id
*ids
)
247 const struct acpi_device_id
*id
;
248 struct acpi_hardware_id
*hwid
;
251 * If the device is not present, it is unnecessary to load device
254 if (!device
->status
.present
)
257 for (id
= ids
; id
->id
[0]; id
++)
258 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
)
259 if (!strcmp((char *) id
->id
, hwid
->id
))
264 EXPORT_SYMBOL(acpi_match_device_ids
);
266 static void acpi_free_ids(struct acpi_device
*device
)
268 struct acpi_hardware_id
*id
, *tmp
;
270 list_for_each_entry_safe(id
, tmp
, &device
->pnp
.ids
, list
) {
276 static void acpi_device_release(struct device
*dev
)
278 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
280 acpi_free_ids(acpi_dev
);
284 static int acpi_device_suspend(struct device
*dev
, pm_message_t state
)
286 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
287 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
289 if (acpi_drv
&& acpi_drv
->ops
.suspend
)
290 return acpi_drv
->ops
.suspend(acpi_dev
, state
);
294 static int acpi_device_resume(struct device
*dev
)
296 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
297 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
299 if (acpi_drv
&& acpi_drv
->ops
.resume
)
300 return acpi_drv
->ops
.resume(acpi_dev
);
304 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
306 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
307 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
309 return !acpi_match_device_ids(acpi_dev
, acpi_drv
->ids
);
312 static int acpi_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
314 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
317 if (add_uevent_var(env
, "MODALIAS="))
319 len
= create_modalias(acpi_dev
, &env
->buf
[env
->buflen
- 1],
320 sizeof(env
->buf
) - env
->buflen
);
321 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
327 static void acpi_device_notify(acpi_handle handle
, u32 event
, void *data
)
329 struct acpi_device
*device
= data
;
331 device
->driver
->ops
.notify(device
, event
);
334 static acpi_status
acpi_device_notify_fixed(void *data
)
336 struct acpi_device
*device
= data
;
338 /* Fixed hardware devices have no handles */
339 acpi_device_notify(NULL
, ACPI_FIXED_HARDWARE_EVENT
, device
);
343 static int acpi_device_install_notify_handler(struct acpi_device
*device
)
347 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
349 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
350 acpi_device_notify_fixed
,
352 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
354 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
355 acpi_device_notify_fixed
,
358 status
= acpi_install_notify_handler(device
->handle
,
363 if (ACPI_FAILURE(status
))
368 static void acpi_device_remove_notify_handler(struct acpi_device
*device
)
370 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
371 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
372 acpi_device_notify_fixed
);
373 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
374 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
375 acpi_device_notify_fixed
);
377 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
381 static int acpi_bus_driver_init(struct acpi_device
*, struct acpi_driver
*);
382 static int acpi_start_single_object(struct acpi_device
*);
383 static int acpi_device_probe(struct device
* dev
)
385 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
386 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
389 ret
= acpi_bus_driver_init(acpi_dev
, acpi_drv
);
391 if (acpi_dev
->bus_ops
.acpi_op_start
)
392 acpi_start_single_object(acpi_dev
);
394 if (acpi_drv
->ops
.notify
) {
395 ret
= acpi_device_install_notify_handler(acpi_dev
);
397 if (acpi_drv
->ops
.remove
)
398 acpi_drv
->ops
.remove(acpi_dev
,
399 acpi_dev
->removal_type
);
404 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
405 "Found driver [%s] for device [%s]\n",
406 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
412 static int acpi_device_remove(struct device
* dev
)
414 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
415 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
418 if (acpi_drv
->ops
.notify
)
419 acpi_device_remove_notify_handler(acpi_dev
);
420 if (acpi_drv
->ops
.remove
)
421 acpi_drv
->ops
.remove(acpi_dev
, acpi_dev
->removal_type
);
423 acpi_dev
->driver
= NULL
;
424 acpi_dev
->driver_data
= NULL
;
430 struct bus_type acpi_bus_type
= {
432 .suspend
= acpi_device_suspend
,
433 .resume
= acpi_device_resume
,
434 .match
= acpi_bus_match
,
435 .probe
= acpi_device_probe
,
436 .remove
= acpi_device_remove
,
437 .uevent
= acpi_device_uevent
,
440 static int acpi_device_register(struct acpi_device
*device
)
443 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
449 * Link this device to its parent and siblings.
451 INIT_LIST_HEAD(&device
->children
);
452 INIT_LIST_HEAD(&device
->node
);
453 INIT_LIST_HEAD(&device
->wakeup_list
);
455 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
457 printk(KERN_ERR PREFIX
"Memory allocation error\n");
461 mutex_lock(&acpi_device_lock
);
463 * Find suitable bus_id and instance number in acpi_bus_id_list
464 * If failed, create one and link it into acpi_bus_id_list
466 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
467 if (!strcmp(acpi_device_bus_id
->bus_id
,
468 acpi_device_hid(device
))) {
469 acpi_device_bus_id
->instance_no
++;
476 acpi_device_bus_id
= new_bus_id
;
477 strcpy(acpi_device_bus_id
->bus_id
, acpi_device_hid(device
));
478 acpi_device_bus_id
->instance_no
= 0;
479 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
481 dev_set_name(&device
->dev
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
484 list_add_tail(&device
->node
, &device
->parent
->children
);
486 if (device
->wakeup
.flags
.valid
)
487 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
488 mutex_unlock(&acpi_device_lock
);
491 device
->dev
.parent
= &device
->parent
->dev
;
492 device
->dev
.bus
= &acpi_bus_type
;
493 device
->dev
.release
= &acpi_device_release
;
494 result
= device_register(&device
->dev
);
496 dev_err(&device
->dev
, "Error registering device\n");
500 result
= acpi_device_setup_files(device
);
502 printk(KERN_ERR PREFIX
"Error creating sysfs interface for device %s\n",
503 dev_name(&device
->dev
));
505 device
->removal_type
= ACPI_BUS_REMOVAL_NORMAL
;
508 mutex_lock(&acpi_device_lock
);
510 list_del(&device
->node
);
511 list_del(&device
->wakeup_list
);
512 mutex_unlock(&acpi_device_lock
);
516 static void acpi_device_unregister(struct acpi_device
*device
, int type
)
518 mutex_lock(&acpi_device_lock
);
520 list_del(&device
->node
);
522 list_del(&device
->wakeup_list
);
523 mutex_unlock(&acpi_device_lock
);
525 acpi_detach_data(device
->handle
, acpi_bus_data_handler
);
527 acpi_device_remove_files(device
);
528 device_unregister(&device
->dev
);
531 /* --------------------------------------------------------------------------
533 -------------------------------------------------------------------------- */
535 * acpi_bus_driver_init - add a device to a driver
536 * @device: the device to add and initialize
537 * @driver: driver for the device
539 * Used to initialize a device via its device driver. Called whenever a
540 * driver is bound to a device. Invokes the driver's add() ops.
543 acpi_bus_driver_init(struct acpi_device
*device
, struct acpi_driver
*driver
)
547 if (!device
|| !driver
)
550 if (!driver
->ops
.add
)
553 result
= driver
->ops
.add(device
);
555 device
->driver
= NULL
;
556 device
->driver_data
= NULL
;
560 device
->driver
= driver
;
563 * TBD - Configuration Management: Assign resources to device based
564 * upon possible configuration and currently allocated resources.
567 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
568 "Driver successfully bound to device\n"));
572 static int acpi_start_single_object(struct acpi_device
*device
)
575 struct acpi_driver
*driver
;
578 if (!(driver
= device
->driver
))
581 if (driver
->ops
.start
) {
582 result
= driver
->ops
.start(device
);
583 if (result
&& driver
->ops
.remove
)
584 driver
->ops
.remove(device
, ACPI_BUS_REMOVAL_NORMAL
);
591 * acpi_bus_register_driver - register a driver with the ACPI bus
592 * @driver: driver being registered
594 * Registers a driver with the ACPI bus. Searches the namespace for all
595 * devices that match the driver's criteria and binds. Returns zero for
596 * success or a negative error status for failure.
598 int acpi_bus_register_driver(struct acpi_driver
*driver
)
604 driver
->drv
.name
= driver
->name
;
605 driver
->drv
.bus
= &acpi_bus_type
;
606 driver
->drv
.owner
= driver
->owner
;
608 ret
= driver_register(&driver
->drv
);
612 EXPORT_SYMBOL(acpi_bus_register_driver
);
615 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
616 * @driver: driver to unregister
618 * Unregisters a driver with the ACPI bus. Searches the namespace for all
619 * devices that match the driver's criteria and unbinds.
621 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
623 driver_unregister(&driver
->drv
);
626 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
628 /* --------------------------------------------------------------------------
630 -------------------------------------------------------------------------- */
631 static struct acpi_device
*acpi_bus_get_parent(acpi_handle handle
)
635 struct acpi_device
*device
;
638 * Fixed hardware devices do not appear in the namespace and do not
639 * have handles, but we fabricate acpi_devices for them, so we have
640 * to deal with them specially.
646 status
= acpi_get_parent(handle
, &handle
);
647 if (status
== AE_NULL_ENTRY
)
649 if (ACPI_FAILURE(status
))
652 ret
= acpi_bus_get_device(handle
, &device
);
659 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
663 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
664 union acpi_object
*obj
;
666 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
667 if (ACPI_FAILURE(status
))
670 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
671 if (ACPI_SUCCESS(status
)) {
672 obj
= buffer
.pointer
;
673 status
= acpi_get_handle(ACPI_ROOT_OBJECT
, obj
->string
.pointer
,
675 kfree(buffer
.pointer
);
679 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
681 void acpi_bus_data_handler(acpi_handle handle
, void *context
)
689 static int acpi_bus_get_perf_flags(struct acpi_device
*device
)
691 device
->performance
.state
= ACPI_STATE_UNKNOWN
;
696 acpi_bus_extract_wakeup_device_power_package(struct acpi_device
*device
,
697 union acpi_object
*package
)
700 union acpi_object
*element
= NULL
;
702 if (!device
|| !package
|| (package
->package
.count
< 2))
703 return AE_BAD_PARAMETER
;
705 element
= &(package
->package
.elements
[0]);
707 return AE_BAD_PARAMETER
;
708 if (element
->type
== ACPI_TYPE_PACKAGE
) {
709 if ((element
->package
.count
< 2) ||
710 (element
->package
.elements
[0].type
!=
711 ACPI_TYPE_LOCAL_REFERENCE
)
712 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
714 device
->wakeup
.gpe_device
=
715 element
->package
.elements
[0].reference
.handle
;
716 device
->wakeup
.gpe_number
=
717 (u32
) element
->package
.elements
[1].integer
.value
;
718 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
719 device
->wakeup
.gpe_number
= element
->integer
.value
;
723 element
= &(package
->package
.elements
[1]);
724 if (element
->type
!= ACPI_TYPE_INTEGER
) {
727 device
->wakeup
.sleep_state
= element
->integer
.value
;
729 if ((package
->package
.count
- 2) > ACPI_MAX_HANDLES
) {
732 device
->wakeup
.resources
.count
= package
->package
.count
- 2;
733 for (i
= 0; i
< device
->wakeup
.resources
.count
; i
++) {
734 element
= &(package
->package
.elements
[i
+ 2]);
735 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
)
738 device
->wakeup
.resources
.handles
[i
] = element
->reference
.handle
;
744 static int acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
746 acpi_status status
= 0;
747 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
748 union acpi_object
*package
= NULL
;
751 struct acpi_device_id button_device_ids
[] = {
759 status
= acpi_evaluate_object(device
->handle
, "_PRW", NULL
, &buffer
);
760 if (ACPI_FAILURE(status
)) {
761 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
765 package
= (union acpi_object
*)buffer
.pointer
;
766 status
= acpi_bus_extract_wakeup_device_power_package(device
, package
);
767 if (ACPI_FAILURE(status
)) {
768 ACPI_EXCEPTION((AE_INFO
, status
, "Extracting _PRW package"));
772 kfree(buffer
.pointer
);
774 device
->wakeup
.flags
.valid
= 1;
775 device
->wakeup
.prepare_count
= 0;
776 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
777 * system for the ACPI device with the _PRW object.
778 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
779 * So it is necessary to call _DSW object first. Only when it is not
780 * present will the _PSW object used.
782 psw_error
= acpi_device_sleep_wake(device
, 0, 0, 0);
784 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
785 "error in _DSW or _PSW evaluation\n"));
787 /* Power button, Lid switch always enable wakeup */
788 if (!acpi_match_device_ids(device
, button_device_ids
))
789 device
->wakeup
.flags
.run_wake
= 1;
792 if (ACPI_FAILURE(status
))
793 device
->flags
.wake_capable
= 0;
797 static int acpi_bus_get_power_flags(struct acpi_device
*device
)
799 acpi_status status
= 0;
800 acpi_handle handle
= NULL
;
805 * Power Management Flags
807 status
= acpi_get_handle(device
->handle
, "_PSC", &handle
);
808 if (ACPI_SUCCESS(status
))
809 device
->power
.flags
.explicit_get
= 1;
810 status
= acpi_get_handle(device
->handle
, "_IRC", &handle
);
811 if (ACPI_SUCCESS(status
))
812 device
->power
.flags
.inrush_current
= 1;
815 * Enumerate supported power management states
817 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3
; i
++) {
818 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
819 char object_name
[5] = { '_', 'P', 'R', '0' + i
, '\0' };
821 /* Evaluate "_PRx" to se if power resources are referenced */
822 acpi_evaluate_reference(device
->handle
, object_name
, NULL
,
824 if (ps
->resources
.count
) {
825 device
->power
.flags
.power_resources
= 1;
829 /* Evaluate "_PSx" to see if we can do explicit sets */
830 object_name
[2] = 'S';
831 status
= acpi_get_handle(device
->handle
, object_name
, &handle
);
832 if (ACPI_SUCCESS(status
)) {
833 ps
->flags
.explicit_set
= 1;
837 /* State is valid if we have some power control */
838 if (ps
->resources
.count
|| ps
->flags
.explicit_set
)
841 ps
->power
= -1; /* Unknown - driver assigned */
842 ps
->latency
= -1; /* Unknown - driver assigned */
845 /* Set defaults for D0 and D3 states (always valid) */
846 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
847 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
848 device
->power
.states
[ACPI_STATE_D3
].flags
.valid
= 1;
849 device
->power
.states
[ACPI_STATE_D3
].power
= 0;
851 /* TBD: System wake support and resource requirements. */
853 device
->power
.state
= ACPI_STATE_UNKNOWN
;
854 acpi_bus_get_power(device
->handle
, &(device
->power
.state
));
859 static int acpi_bus_get_flags(struct acpi_device
*device
)
861 acpi_status status
= AE_OK
;
862 acpi_handle temp
= NULL
;
865 /* Presence of _STA indicates 'dynamic_status' */
866 status
= acpi_get_handle(device
->handle
, "_STA", &temp
);
867 if (ACPI_SUCCESS(status
))
868 device
->flags
.dynamic_status
= 1;
870 /* Presence of _RMV indicates 'removable' */
871 status
= acpi_get_handle(device
->handle
, "_RMV", &temp
);
872 if (ACPI_SUCCESS(status
))
873 device
->flags
.removable
= 1;
875 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
876 status
= acpi_get_handle(device
->handle
, "_EJD", &temp
);
877 if (ACPI_SUCCESS(status
))
878 device
->flags
.ejectable
= 1;
880 status
= acpi_get_handle(device
->handle
, "_EJ0", &temp
);
881 if (ACPI_SUCCESS(status
))
882 device
->flags
.ejectable
= 1;
885 /* Presence of _LCK indicates 'lockable' */
886 status
= acpi_get_handle(device
->handle
, "_LCK", &temp
);
887 if (ACPI_SUCCESS(status
))
888 device
->flags
.lockable
= 1;
890 /* Presence of _PS0|_PR0 indicates 'power manageable' */
891 status
= acpi_get_handle(device
->handle
, "_PS0", &temp
);
892 if (ACPI_FAILURE(status
))
893 status
= acpi_get_handle(device
->handle
, "_PR0", &temp
);
894 if (ACPI_SUCCESS(status
))
895 device
->flags
.power_manageable
= 1;
897 /* Presence of _PRW indicates wake capable */
898 status
= acpi_get_handle(device
->handle
, "_PRW", &temp
);
899 if (ACPI_SUCCESS(status
))
900 device
->flags
.wake_capable
= 1;
902 /* TBD: Performance management */
907 static void acpi_device_get_busid(struct acpi_device
*device
)
909 char bus_id
[5] = { '?', 0 };
910 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
916 * The device's Bus ID is simply the object name.
917 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
919 if (ACPI_IS_ROOT_DEVICE(device
)) {
920 strcpy(device
->pnp
.bus_id
, "ACPI");
924 switch (device
->device_type
) {
925 case ACPI_BUS_TYPE_POWER_BUTTON
:
926 strcpy(device
->pnp
.bus_id
, "PWRF");
928 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
929 strcpy(device
->pnp
.bus_id
, "SLPF");
932 acpi_get_name(device
->handle
, ACPI_SINGLE_NAME
, &buffer
);
933 /* Clean up trailing underscores (if any) */
934 for (i
= 3; i
> 1; i
--) {
935 if (bus_id
[i
] == '_')
940 strcpy(device
->pnp
.bus_id
, bus_id
);
946 * acpi_bay_match - see if a device is an ejectable driver bay
948 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
949 * then we can safely call it an ejectable drive bay
951 static int acpi_bay_match(struct acpi_device
*device
){
957 handle
= device
->handle
;
959 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
960 if (ACPI_FAILURE(status
))
963 if ((ACPI_SUCCESS(acpi_get_handle(handle
, "_GTF", &tmp
))) ||
964 (ACPI_SUCCESS(acpi_get_handle(handle
, "_GTM", &tmp
))) ||
965 (ACPI_SUCCESS(acpi_get_handle(handle
, "_STM", &tmp
))) ||
966 (ACPI_SUCCESS(acpi_get_handle(handle
, "_SDD", &tmp
))))
969 if (acpi_get_parent(handle
, &phandle
))
972 if ((ACPI_SUCCESS(acpi_get_handle(phandle
, "_GTF", &tmp
))) ||
973 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_GTM", &tmp
))) ||
974 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_STM", &tmp
))) ||
975 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_SDD", &tmp
))))
982 * acpi_dock_match - see if a device has a _DCK method
984 static int acpi_dock_match(struct acpi_device
*device
)
987 return acpi_get_handle(device
->handle
, "_DCK", &tmp
);
990 char *acpi_device_hid(struct acpi_device
*device
)
992 struct acpi_hardware_id
*hid
;
994 hid
= list_first_entry(&device
->pnp
.ids
, struct acpi_hardware_id
, list
);
997 EXPORT_SYMBOL(acpi_device_hid
);
999 static void acpi_add_id(struct acpi_device
*device
, const char *dev_id
)
1001 struct acpi_hardware_id
*id
;
1003 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
1007 id
->id
= kmalloc(strlen(dev_id
) + 1, GFP_KERNEL
);
1013 strcpy(id
->id
, dev_id
);
1014 list_add_tail(&id
->list
, &device
->pnp
.ids
);
1017 static void acpi_device_set_id(struct acpi_device
*device
)
1020 struct acpi_device_info
*info
;
1021 struct acpica_device_id_list
*cid_list
;
1024 switch (device
->device_type
) {
1025 case ACPI_BUS_TYPE_DEVICE
:
1026 if (ACPI_IS_ROOT_DEVICE(device
)) {
1027 acpi_add_id(device
, ACPI_SYSTEM_HID
);
1029 } else if (ACPI_IS_ROOT_DEVICE(device
->parent
)) {
1030 /* \_SB_, the only root-level namespace device */
1031 acpi_add_id(device
, ACPI_BUS_HID
);
1032 strcpy(device
->pnp
.device_name
, ACPI_BUS_DEVICE_NAME
);
1033 strcpy(device
->pnp
.device_class
, ACPI_BUS_CLASS
);
1037 status
= acpi_get_object_info(device
->handle
, &info
);
1038 if (ACPI_FAILURE(status
)) {
1039 printk(KERN_ERR PREFIX
"%s: Error reading device info\n", __func__
);
1043 if (info
->valid
& ACPI_VALID_HID
)
1044 acpi_add_id(device
, info
->hardware_id
.string
);
1045 if (info
->valid
& ACPI_VALID_CID
) {
1046 cid_list
= &info
->compatible_id_list
;
1047 for (i
= 0; i
< cid_list
->count
; i
++)
1048 acpi_add_id(device
, cid_list
->ids
[i
].string
);
1050 if (info
->valid
& ACPI_VALID_ADR
) {
1051 device
->pnp
.bus_address
= info
->address
;
1052 device
->flags
.bus_address
= 1;
1058 * Some devices don't reliably have _HIDs & _CIDs, so add
1059 * synthetic HIDs to make sure drivers can find them.
1061 if (acpi_is_video_device(device
))
1062 acpi_add_id(device
, ACPI_VIDEO_HID
);
1063 else if (ACPI_SUCCESS(acpi_bay_match(device
)))
1064 acpi_add_id(device
, ACPI_BAY_HID
);
1065 else if (ACPI_SUCCESS(acpi_dock_match(device
)))
1066 acpi_add_id(device
, ACPI_DOCK_HID
);
1069 case ACPI_BUS_TYPE_POWER
:
1070 acpi_add_id(device
, ACPI_POWER_HID
);
1072 case ACPI_BUS_TYPE_PROCESSOR
:
1073 acpi_add_id(device
, ACPI_PROCESSOR_OBJECT_HID
);
1075 case ACPI_BUS_TYPE_THERMAL
:
1076 acpi_add_id(device
, ACPI_THERMAL_HID
);
1078 case ACPI_BUS_TYPE_POWER_BUTTON
:
1079 acpi_add_id(device
, ACPI_BUTTON_HID_POWERF
);
1081 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1082 acpi_add_id(device
, ACPI_BUTTON_HID_SLEEPF
);
1087 * We build acpi_devices for some objects that don't have _HID or _CID,
1088 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1089 * but we do use them indirectly by traversing the acpi_device tree.
1090 * This generic ID isn't useful for driver binding, but it provides
1091 * the useful property that "every acpi_device has an ID."
1093 if (list_empty(&device
->pnp
.ids
))
1094 acpi_add_id(device
, "device");
1097 static int acpi_device_set_context(struct acpi_device
*device
)
1104 * Attach this 'struct acpi_device' to the ACPI object. This makes
1105 * resolutions from handle->device very efficient. Fixed hardware
1106 * devices have no handles, so we skip them.
1108 if (!device
->handle
)
1111 status
= acpi_attach_data(device
->handle
,
1112 acpi_bus_data_handler
, device
);
1113 if (ACPI_SUCCESS(status
))
1116 printk(KERN_ERR PREFIX
"Error attaching device data\n");
1120 static int acpi_bus_remove(struct acpi_device
*dev
, int rmdevice
)
1125 dev
->removal_type
= ACPI_BUS_REMOVAL_EJECT
;
1126 device_release_driver(&dev
->dev
);
1132 * unbind _ADR-Based Devices when hot removal
1134 if (dev
->flags
.bus_address
) {
1135 if ((dev
->parent
) && (dev
->parent
->ops
.unbind
))
1136 dev
->parent
->ops
.unbind(dev
);
1138 acpi_device_unregister(dev
, ACPI_BUS_REMOVAL_EJECT
);
1143 static int acpi_add_single_object(struct acpi_device
**child
,
1144 acpi_handle handle
, int type
,
1145 unsigned long long sta
,
1146 struct acpi_bus_ops
*ops
)
1149 struct acpi_device
*device
;
1150 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1152 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
1154 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1158 INIT_LIST_HEAD(&device
->pnp
.ids
);
1159 device
->device_type
= type
;
1160 device
->handle
= handle
;
1161 device
->parent
= acpi_bus_get_parent(handle
);
1162 device
->bus_ops
= *ops
; /* workround for not call .start */
1163 STRUCT_TO_INT(device
->status
) = sta
;
1165 acpi_device_get_busid(device
);
1170 * Note that we only look for object handles -- cannot evaluate objects
1171 * until we know the device is present and properly initialized.
1173 result
= acpi_bus_get_flags(device
);
1180 * TBD: Synch with Core's enumeration/initialization process.
1182 acpi_device_set_id(device
);
1188 if (device
->flags
.power_manageable
) {
1189 result
= acpi_bus_get_power_flags(device
);
1195 * Wakeup device management
1196 *-----------------------
1198 if (device
->flags
.wake_capable
) {
1199 result
= acpi_bus_get_wakeup_device_flags(device
);
1205 * Performance Management
1206 * ----------------------
1208 if (device
->flags
.performance_manageable
) {
1209 result
= acpi_bus_get_perf_flags(device
);
1214 if ((result
= acpi_device_set_context(device
)))
1217 result
= acpi_device_register(device
);
1220 * Bind _ADR-Based Devices when hot add
1222 if (device
->flags
.bus_address
) {
1223 if (device
->parent
&& device
->parent
->ops
.bind
)
1224 device
->parent
->ops
.bind(device
);
1229 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
1230 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1231 "Adding %s [%s] parent %s\n", dev_name(&device
->dev
),
1232 (char *) buffer
.pointer
,
1233 device
->parent
? dev_name(&device
->parent
->dev
) :
1235 kfree(buffer
.pointer
);
1238 acpi_device_release(&device
->dev
);
1243 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1244 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1246 static int acpi_bus_type_and_status(acpi_handle handle
, int *type
,
1247 unsigned long long *sta
)
1250 acpi_object_type acpi_type
;
1252 status
= acpi_get_type(handle
, &acpi_type
);
1253 if (ACPI_FAILURE(status
))
1256 switch (acpi_type
) {
1257 case ACPI_TYPE_ANY
: /* for ACPI_ROOT_OBJECT */
1258 case ACPI_TYPE_DEVICE
:
1259 *type
= ACPI_BUS_TYPE_DEVICE
;
1260 status
= acpi_bus_get_status_handle(handle
, sta
);
1261 if (ACPI_FAILURE(status
))
1264 case ACPI_TYPE_PROCESSOR
:
1265 *type
= ACPI_BUS_TYPE_PROCESSOR
;
1266 status
= acpi_bus_get_status_handle(handle
, sta
);
1267 if (ACPI_FAILURE(status
))
1270 case ACPI_TYPE_THERMAL
:
1271 *type
= ACPI_BUS_TYPE_THERMAL
;
1272 *sta
= ACPI_STA_DEFAULT
;
1274 case ACPI_TYPE_POWER
:
1275 *type
= ACPI_BUS_TYPE_POWER
;
1276 *sta
= ACPI_STA_DEFAULT
;
1285 static acpi_status
acpi_bus_check_add(acpi_handle handle
, u32 lvl
,
1286 void *context
, void **return_value
)
1288 struct acpi_bus_ops
*ops
= context
;
1290 unsigned long long sta
;
1291 struct acpi_device
*device
;
1295 result
= acpi_bus_type_and_status(handle
, &type
, &sta
);
1299 if (!(sta
& ACPI_STA_DEVICE_PRESENT
) &&
1300 !(sta
& ACPI_STA_DEVICE_FUNCTIONING
))
1301 return AE_CTRL_DEPTH
;
1304 * We may already have an acpi_device from a previous enumeration. If
1305 * so, we needn't add it again, but we may still have to start it.
1308 acpi_bus_get_device(handle
, &device
);
1309 if (ops
->acpi_op_add
&& !device
)
1310 acpi_add_single_object(&device
, handle
, type
, sta
, ops
);
1313 return AE_CTRL_DEPTH
;
1315 if (ops
->acpi_op_start
&& !(ops
->acpi_op_add
)) {
1316 status
= acpi_start_single_object(device
);
1317 if (ACPI_FAILURE(status
))
1318 return AE_CTRL_DEPTH
;
1322 *return_value
= device
;
1326 static int acpi_bus_scan(acpi_handle handle
, struct acpi_bus_ops
*ops
,
1327 struct acpi_device
**child
)
1330 void *device
= NULL
;
1332 status
= acpi_bus_check_add(handle
, 0, ops
, &device
);
1333 if (ACPI_SUCCESS(status
))
1334 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
1335 acpi_bus_check_add
, ops
, &device
);
1343 acpi_bus_add(struct acpi_device
**child
,
1344 struct acpi_device
*parent
, acpi_handle handle
, int type
)
1346 struct acpi_bus_ops ops
;
1348 memset(&ops
, 0, sizeof(ops
));
1349 ops
.acpi_op_add
= 1;
1351 acpi_bus_scan(handle
, &ops
, child
);
1354 EXPORT_SYMBOL(acpi_bus_add
);
1356 int acpi_bus_start(struct acpi_device
*device
)
1358 struct acpi_bus_ops ops
;
1360 memset(&ops
, 0, sizeof(ops
));
1361 ops
.acpi_op_start
= 1;
1363 acpi_bus_scan(device
->handle
, &ops
, NULL
);
1366 EXPORT_SYMBOL(acpi_bus_start
);
1368 int acpi_bus_trim(struct acpi_device
*start
, int rmdevice
)
1371 struct acpi_device
*parent
, *child
;
1372 acpi_handle phandle
, chandle
;
1373 acpi_object_type type
;
1378 phandle
= start
->handle
;
1379 child
= chandle
= NULL
;
1381 while ((level
> 0) && parent
&& (!err
)) {
1382 status
= acpi_get_next_object(ACPI_TYPE_ANY
, phandle
,
1386 * If this scope is exhausted then move our way back up.
1388 if (ACPI_FAILURE(status
)) {
1391 acpi_get_parent(phandle
, &phandle
);
1393 parent
= parent
->parent
;
1396 err
= acpi_bus_remove(child
, rmdevice
);
1398 err
= acpi_bus_remove(child
, 1);
1403 status
= acpi_get_type(chandle
, &type
);
1404 if (ACPI_FAILURE(status
)) {
1408 * If there is a device corresponding to chandle then
1409 * parse it (depth-first).
1411 if (acpi_bus_get_device(chandle
, &child
) == 0) {
1421 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
1423 static int acpi_bus_scan_fixed(void)
1426 struct acpi_device
*device
= NULL
;
1427 struct acpi_bus_ops ops
;
1429 memset(&ops
, 0, sizeof(ops
));
1430 ops
.acpi_op_add
= 1;
1431 ops
.acpi_op_start
= 1;
1434 * Enumerate all fixed-feature devices.
1436 if ((acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
) == 0) {
1437 result
= acpi_add_single_object(&device
, NULL
,
1438 ACPI_BUS_TYPE_POWER_BUTTON
,
1443 if ((acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
) == 0) {
1444 result
= acpi_add_single_object(&device
, NULL
,
1445 ACPI_BUS_TYPE_SLEEP_BUTTON
,
1453 int __init
acpi_scan_init(void)
1456 struct acpi_bus_ops ops
;
1458 memset(&ops
, 0, sizeof(ops
));
1459 ops
.acpi_op_add
= 1;
1460 ops
.acpi_op_start
= 1;
1462 result
= bus_register(&acpi_bus_type
);
1464 /* We don't want to quit even if we failed to add suspend/resume */
1465 printk(KERN_ERR PREFIX
"Could not register bus type\n");
1469 * Enumerate devices in the ACPI namespace.
1471 result
= acpi_bus_scan(ACPI_ROOT_OBJECT
, &ops
, &acpi_root
);
1474 result
= acpi_bus_scan_fixed();
1477 acpi_device_unregister(acpi_root
, ACPI_BUS_REMOVAL_NORMAL
);