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 <asm/signal.h>
11 #include <acpi/acpi_drivers.h>
12 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
14 #define _COMPONENT ACPI_BUS_COMPONENT
15 ACPI_MODULE_NAME("scan");
16 #define STRUCT_TO_INT(s) (*((int*)&s))
17 extern struct acpi_device
*acpi_root
;
19 #define ACPI_BUS_CLASS "system_bus"
20 #define ACPI_BUS_HID "LNXSYBUS"
21 #define ACPI_BUS_DEVICE_NAME "System Bus"
23 static LIST_HEAD(acpi_device_list
);
24 static LIST_HEAD(acpi_bus_id_list
);
25 DEFINE_SPINLOCK(acpi_device_lock
);
26 LIST_HEAD(acpi_wakeup_device_list
);
28 struct acpi_device_bus_id
{
30 unsigned int instance_no
;
31 struct list_head node
;
35 * Creates hid/cid(s) string needed for modalias and uevent
36 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
37 * char *modalias: "acpi:IBM0001:ACPI0001"
39 static int create_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
45 if (!acpi_dev
->flags
.hardware_id
&& !acpi_dev
->flags
.compatible_ids
)
48 len
= snprintf(modalias
, size
, "acpi:");
51 if (acpi_dev
->flags
.hardware_id
) {
52 count
= snprintf(&modalias
[len
], size
, "%s:",
53 acpi_dev
->pnp
.hardware_id
);
54 if (count
< 0 || count
>= size
)
60 if (acpi_dev
->flags
.compatible_ids
) {
61 struct acpi_compatible_id_list
*cid_list
;
64 cid_list
= acpi_dev
->pnp
.cid_list
;
65 for (i
= 0; i
< cid_list
->count
; i
++) {
66 count
= snprintf(&modalias
[len
], size
, "%s:",
67 cid_list
->id
[i
].value
);
68 if (count
< 0 || count
>= size
) {
69 printk(KERN_ERR PREFIX
"%s cid[%i] exceeds event buffer size",
70 acpi_dev
->pnp
.device_name
, i
);
83 acpi_device_modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
84 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
87 /* Device has no HID and no CID or string is >1024 */
88 len
= create_modalias(acpi_dev
, buf
, 1024);
94 static DEVICE_ATTR(modalias
, 0444, acpi_device_modalias_show
, NULL
);
96 static int acpi_bus_hot_remove_device(void *context
)
98 struct acpi_device
*device
;
99 acpi_handle handle
= context
;
100 struct acpi_object_list arg_list
;
101 union acpi_object arg
;
102 acpi_status status
= AE_OK
;
104 if (acpi_bus_get_device(handle
, &device
))
110 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
111 "Hot-removing device %s...\n", device
->dev
.bus_id
));
114 if (acpi_bus_trim(device
, 1)) {
115 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
116 "Removing device failed\n"));
120 /* power off device */
121 status
= acpi_evaluate_object(handle
, "_PS3", NULL
, NULL
);
122 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
)
123 ACPI_DEBUG_PRINT((ACPI_DB_WARN
,
124 "Power-off device failed\n"));
126 if (device
->flags
.lockable
) {
128 arg_list
.pointer
= &arg
;
129 arg
.type
= ACPI_TYPE_INTEGER
;
130 arg
.integer
.value
= 0;
131 acpi_evaluate_object(handle
, "_LCK", &arg_list
, NULL
);
135 arg_list
.pointer
= &arg
;
136 arg
.type
= ACPI_TYPE_INTEGER
;
137 arg
.integer
.value
= 1;
142 status
= acpi_evaluate_object(handle
, "_EJ0", &arg_list
, NULL
);
143 if (ACPI_FAILURE(status
))
150 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
151 const char *buf
, size_t count
)
155 acpi_object_type type
= 0;
156 struct acpi_device
*acpi_device
= to_acpi_device(d
);
158 if ((!count
) || (buf
[0] != '1')) {
162 if (acpi_device
->driver
== NULL
) {
167 status
= acpi_get_type(acpi_device
->handle
, &type
);
168 if (ACPI_FAILURE(status
) || (!acpi_device
->flags
.ejectable
)) {
173 /* remove the device in another thread to fix the deadlock issue */
174 ret
= kernel_thread(acpi_bus_hot_remove_device
,
175 acpi_device
->handle
, SIGCHLD
);
180 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
183 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
184 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
186 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.hardware_id
);
188 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
191 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
192 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
193 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
196 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
200 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
205 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
207 static int acpi_device_setup_files(struct acpi_device
*dev
)
214 * Devices gotten from FADT don't have a "path" attribute
217 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
222 if(dev
->flags
.hardware_id
) {
223 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
228 if (dev
->flags
.hardware_id
|| dev
->flags
.compatible_ids
){
229 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
235 * If device has _EJ0, 'eject' file is created that is used to trigger
236 * hot-removal function from userland.
238 status
= acpi_get_handle(dev
->handle
, "_EJ0", &temp
);
239 if (ACPI_SUCCESS(status
))
240 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
245 static void acpi_device_remove_files(struct acpi_device
*dev
)
251 * If device has _EJ0, 'eject' file is created that is used to trigger
252 * hot-removal function from userland.
254 status
= acpi_get_handle(dev
->handle
, "_EJ0", &temp
);
255 if (ACPI_SUCCESS(status
))
256 device_remove_file(&dev
->dev
, &dev_attr_eject
);
258 if (dev
->flags
.hardware_id
|| dev
->flags
.compatible_ids
)
259 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
261 if(dev
->flags
.hardware_id
)
262 device_remove_file(&dev
->dev
, &dev_attr_hid
);
264 device_remove_file(&dev
->dev
, &dev_attr_path
);
266 /* --------------------------------------------------------------------------
268 -------------------------------------------------------------------------- */
270 int acpi_match_device_ids(struct acpi_device
*device
,
271 const struct acpi_device_id
*ids
)
273 const struct acpi_device_id
*id
;
275 if (device
->flags
.hardware_id
) {
276 for (id
= ids
; id
->id
[0]; id
++) {
277 if (!strcmp((char*)id
->id
, device
->pnp
.hardware_id
))
282 if (device
->flags
.compatible_ids
) {
283 struct acpi_compatible_id_list
*cid_list
= device
->pnp
.cid_list
;
286 for (id
= ids
; id
->id
[0]; id
++) {
287 /* compare multiple _CID entries against driver ids */
288 for (i
= 0; i
< cid_list
->count
; i
++) {
289 if (!strcmp((char*)id
->id
,
290 cid_list
->id
[i
].value
))
298 EXPORT_SYMBOL(acpi_match_device_ids
);
300 static void acpi_device_release(struct device
*dev
)
302 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
304 kfree(acpi_dev
->pnp
.cid_list
);
308 static int acpi_device_suspend(struct device
*dev
, pm_message_t state
)
310 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
311 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
313 if (acpi_drv
&& acpi_drv
->ops
.suspend
)
314 return acpi_drv
->ops
.suspend(acpi_dev
, state
);
318 static int acpi_device_resume(struct device
*dev
)
320 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
321 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
323 if (acpi_drv
&& acpi_drv
->ops
.resume
)
324 return acpi_drv
->ops
.resume(acpi_dev
);
328 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
330 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
331 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
333 return !acpi_match_device_ids(acpi_dev
, acpi_drv
->ids
);
336 static int acpi_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
338 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
341 if (add_uevent_var(env
, "MODALIAS="))
343 len
= create_modalias(acpi_dev
, &env
->buf
[env
->buflen
- 1],
344 sizeof(env
->buf
) - env
->buflen
);
345 if (len
>= (sizeof(env
->buf
) - env
->buflen
))
351 static int acpi_bus_driver_init(struct acpi_device
*, struct acpi_driver
*);
352 static int acpi_start_single_object(struct acpi_device
*);
353 static int acpi_device_probe(struct device
* dev
)
355 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
356 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
359 ret
= acpi_bus_driver_init(acpi_dev
, acpi_drv
);
361 if (acpi_dev
->bus_ops
.acpi_op_start
)
362 acpi_start_single_object(acpi_dev
);
363 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
364 "Found driver [%s] for device [%s]\n",
365 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
371 static int acpi_device_remove(struct device
* dev
)
373 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
374 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
377 if (acpi_drv
->ops
.stop
)
378 acpi_drv
->ops
.stop(acpi_dev
, acpi_dev
->removal_type
);
379 if (acpi_drv
->ops
.remove
)
380 acpi_drv
->ops
.remove(acpi_dev
, acpi_dev
->removal_type
);
382 acpi_dev
->driver
= NULL
;
383 acpi_driver_data(dev
) = NULL
;
389 static void acpi_device_shutdown(struct device
*dev
)
391 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
392 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
394 if (acpi_drv
&& acpi_drv
->ops
.shutdown
)
395 acpi_drv
->ops
.shutdown(acpi_dev
);
400 struct bus_type acpi_bus_type
= {
402 .suspend
= acpi_device_suspend
,
403 .resume
= acpi_device_resume
,
404 .shutdown
= acpi_device_shutdown
,
405 .match
= acpi_bus_match
,
406 .probe
= acpi_device_probe
,
407 .remove
= acpi_device_remove
,
408 .uevent
= acpi_device_uevent
,
411 static int acpi_device_register(struct acpi_device
*device
,
412 struct acpi_device
*parent
)
415 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
420 * Link this device to its parent and siblings.
422 INIT_LIST_HEAD(&device
->children
);
423 INIT_LIST_HEAD(&device
->node
);
424 INIT_LIST_HEAD(&device
->g_list
);
425 INIT_LIST_HEAD(&device
->wakeup_list
);
427 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
429 printk(KERN_ERR PREFIX
"Memory allocation error\n");
433 spin_lock(&acpi_device_lock
);
435 * Find suitable bus_id and instance number in acpi_bus_id_list
436 * If failed, create one and link it into acpi_bus_id_list
438 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
439 if(!strcmp(acpi_device_bus_id
->bus_id
, device
->flags
.hardware_id
? device
->pnp
.hardware_id
: "device")) {
440 acpi_device_bus_id
->instance_no
++;
447 acpi_device_bus_id
= new_bus_id
;
448 strcpy(acpi_device_bus_id
->bus_id
, device
->flags
.hardware_id
? device
->pnp
.hardware_id
: "device");
449 acpi_device_bus_id
->instance_no
= 0;
450 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
452 sprintf(device
->dev
.bus_id
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
454 if (device
->parent
) {
455 list_add_tail(&device
->node
, &device
->parent
->children
);
456 list_add_tail(&device
->g_list
, &device
->parent
->g_list
);
458 list_add_tail(&device
->g_list
, &acpi_device_list
);
459 if (device
->wakeup
.flags
.valid
)
460 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
461 spin_unlock(&acpi_device_lock
);
464 device
->dev
.parent
= &parent
->dev
;
465 device
->dev
.bus
= &acpi_bus_type
;
466 device_initialize(&device
->dev
);
467 device
->dev
.release
= &acpi_device_release
;
468 result
= device_add(&device
->dev
);
470 printk(KERN_ERR PREFIX
"Error adding device %s", device
->dev
.bus_id
);
474 result
= acpi_device_setup_files(device
);
476 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error creating sysfs interface for device %s\n", device
->dev
.bus_id
));
478 device
->removal_type
= ACPI_BUS_REMOVAL_NORMAL
;
481 spin_lock(&acpi_device_lock
);
482 if (device
->parent
) {
483 list_del(&device
->node
);
484 list_del(&device
->g_list
);
486 list_del(&device
->g_list
);
487 list_del(&device
->wakeup_list
);
488 spin_unlock(&acpi_device_lock
);
492 static void acpi_device_unregister(struct acpi_device
*device
, int type
)
494 spin_lock(&acpi_device_lock
);
495 if (device
->parent
) {
496 list_del(&device
->node
);
497 list_del(&device
->g_list
);
499 list_del(&device
->g_list
);
501 list_del(&device
->wakeup_list
);
502 spin_unlock(&acpi_device_lock
);
504 acpi_detach_data(device
->handle
, acpi_bus_data_handler
);
506 acpi_device_remove_files(device
);
507 device_unregister(&device
->dev
);
510 /* --------------------------------------------------------------------------
512 -------------------------------------------------------------------------- */
514 * acpi_bus_driver_init - add a device to a driver
515 * @device: the device to add and initialize
516 * @driver: driver for the device
518 * Used to initialize a device via its device driver. Called whenever a
519 * driver is bound to a device. Invokes the driver's add() ops.
522 acpi_bus_driver_init(struct acpi_device
*device
, struct acpi_driver
*driver
)
527 if (!device
|| !driver
)
530 if (!driver
->ops
.add
)
533 result
= driver
->ops
.add(device
);
535 device
->driver
= NULL
;
536 acpi_driver_data(device
) = NULL
;
540 device
->driver
= driver
;
543 * TBD - Configuration Management: Assign resources to device based
544 * upon possible configuration and currently allocated resources.
547 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
548 "Driver successfully bound to device\n"));
552 static int acpi_start_single_object(struct acpi_device
*device
)
555 struct acpi_driver
*driver
;
558 if (!(driver
= device
->driver
))
561 if (driver
->ops
.start
) {
562 result
= driver
->ops
.start(device
);
563 if (result
&& driver
->ops
.remove
)
564 driver
->ops
.remove(device
, ACPI_BUS_REMOVAL_NORMAL
);
571 * acpi_bus_register_driver - register a driver with the ACPI bus
572 * @driver: driver being registered
574 * Registers a driver with the ACPI bus. Searches the namespace for all
575 * devices that match the driver's criteria and binds. Returns zero for
576 * success or a negative error status for failure.
578 int acpi_bus_register_driver(struct acpi_driver
*driver
)
584 driver
->drv
.name
= driver
->name
;
585 driver
->drv
.bus
= &acpi_bus_type
;
586 driver
->drv
.owner
= driver
->owner
;
588 ret
= driver_register(&driver
->drv
);
592 EXPORT_SYMBOL(acpi_bus_register_driver
);
595 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
596 * @driver: driver to unregister
598 * Unregisters a driver with the ACPI bus. Searches the namespace for all
599 * devices that match the driver's criteria and unbinds.
601 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
603 driver_unregister(&driver
->drv
);
606 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
608 /* --------------------------------------------------------------------------
610 -------------------------------------------------------------------------- */
612 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
616 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
617 union acpi_object
*obj
;
619 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
620 if (ACPI_FAILURE(status
))
623 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
624 if (ACPI_SUCCESS(status
)) {
625 obj
= buffer
.pointer
;
626 status
= acpi_get_handle(ACPI_ROOT_OBJECT
, obj
->string
.pointer
,
628 kfree(buffer
.pointer
);
632 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
634 void acpi_bus_data_handler(acpi_handle handle
, u32 function
, void *context
)
642 static int acpi_bus_get_perf_flags(struct acpi_device
*device
)
644 device
->performance
.state
= ACPI_STATE_UNKNOWN
;
649 acpi_bus_extract_wakeup_device_power_package(struct acpi_device
*device
,
650 union acpi_object
*package
)
653 union acpi_object
*element
= NULL
;
655 if (!device
|| !package
|| (package
->package
.count
< 2))
656 return AE_BAD_PARAMETER
;
658 element
= &(package
->package
.elements
[0]);
660 return AE_BAD_PARAMETER
;
661 if (element
->type
== ACPI_TYPE_PACKAGE
) {
662 if ((element
->package
.count
< 2) ||
663 (element
->package
.elements
[0].type
!=
664 ACPI_TYPE_LOCAL_REFERENCE
)
665 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
667 device
->wakeup
.gpe_device
=
668 element
->package
.elements
[0].reference
.handle
;
669 device
->wakeup
.gpe_number
=
670 (u32
) element
->package
.elements
[1].integer
.value
;
671 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
672 device
->wakeup
.gpe_number
= element
->integer
.value
;
676 element
= &(package
->package
.elements
[1]);
677 if (element
->type
!= ACPI_TYPE_INTEGER
) {
680 device
->wakeup
.sleep_state
= element
->integer
.value
;
682 if ((package
->package
.count
- 2) > ACPI_MAX_HANDLES
) {
685 device
->wakeup
.resources
.count
= package
->package
.count
- 2;
686 for (i
= 0; i
< device
->wakeup
.resources
.count
; i
++) {
687 element
= &(package
->package
.elements
[i
+ 2]);
688 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
)
691 device
->wakeup
.resources
.handles
[i
] = element
->reference
.handle
;
697 static int acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
699 acpi_status status
= 0;
700 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
701 union acpi_object
*package
= NULL
;
702 union acpi_object in_arg
[3];
703 struct acpi_object_list arg_list
= { 3, in_arg
};
704 acpi_status psw_status
= AE_OK
;
706 struct acpi_device_id button_device_ids
[] = {
714 status
= acpi_evaluate_object(device
->handle
, "_PRW", NULL
, &buffer
);
715 if (ACPI_FAILURE(status
)) {
716 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
720 package
= (union acpi_object
*)buffer
.pointer
;
721 status
= acpi_bus_extract_wakeup_device_power_package(device
, package
);
722 if (ACPI_FAILURE(status
)) {
723 ACPI_EXCEPTION((AE_INFO
, status
, "Extracting _PRW package"));
727 kfree(buffer
.pointer
);
729 device
->wakeup
.flags
.valid
= 1;
730 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
731 * system for the ACPI device with the _PRW object.
732 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
733 * So it is necessary to call _DSW object first. Only when it is not
734 * present will the _PSW object used.
737 * Three agruments are needed for the _DSW object.
738 * Argument 0: enable/disable the wake capabilities
739 * When _DSW object is called to disable the wake capabilities, maybe
740 * the first argument is filled. The value of the other two agruments
743 in_arg
[0].type
= ACPI_TYPE_INTEGER
;
744 in_arg
[0].integer
.value
= 0;
745 in_arg
[1].type
= ACPI_TYPE_INTEGER
;
746 in_arg
[1].integer
.value
= 0;
747 in_arg
[2].type
= ACPI_TYPE_INTEGER
;
748 in_arg
[2].integer
.value
= 0;
749 psw_status
= acpi_evaluate_object(device
->handle
, "_DSW",
751 if (ACPI_FAILURE(psw_status
) && (psw_status
!= AE_NOT_FOUND
))
752 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "error in evaluate _DSW\n"));
754 * When the _DSW object is not present, OSPM will call _PSW object.
756 if (psw_status
== AE_NOT_FOUND
) {
758 * Only one agruments is required for the _PSW object.
759 * agrument 0: enable/disable the wake capabilities
762 in_arg
[0].integer
.value
= 0;
763 psw_status
= acpi_evaluate_object(device
->handle
, "_PSW",
765 if (ACPI_FAILURE(psw_status
) && (psw_status
!= AE_NOT_FOUND
))
766 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "error in "
769 /* Power button, Lid switch always enable wakeup */
770 if (!acpi_match_device_ids(device
, button_device_ids
))
771 device
->wakeup
.flags
.run_wake
= 1;
774 if (ACPI_FAILURE(status
))
775 device
->flags
.wake_capable
= 0;
779 static int acpi_bus_get_power_flags(struct acpi_device
*device
)
781 acpi_status status
= 0;
782 acpi_handle handle
= NULL
;
787 * Power Management Flags
789 status
= acpi_get_handle(device
->handle
, "_PSC", &handle
);
790 if (ACPI_SUCCESS(status
))
791 device
->power
.flags
.explicit_get
= 1;
792 status
= acpi_get_handle(device
->handle
, "_IRC", &handle
);
793 if (ACPI_SUCCESS(status
))
794 device
->power
.flags
.inrush_current
= 1;
797 * Enumerate supported power management states
799 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3
; i
++) {
800 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
801 char object_name
[5] = { '_', 'P', 'R', '0' + i
, '\0' };
803 /* Evaluate "_PRx" to se if power resources are referenced */
804 acpi_evaluate_reference(device
->handle
, object_name
, NULL
,
806 if (ps
->resources
.count
) {
807 device
->power
.flags
.power_resources
= 1;
811 /* Evaluate "_PSx" to see if we can do explicit sets */
812 object_name
[2] = 'S';
813 status
= acpi_get_handle(device
->handle
, object_name
, &handle
);
814 if (ACPI_SUCCESS(status
)) {
815 ps
->flags
.explicit_set
= 1;
819 /* State is valid if we have some power control */
820 if (ps
->resources
.count
|| ps
->flags
.explicit_set
)
823 ps
->power
= -1; /* Unknown - driver assigned */
824 ps
->latency
= -1; /* Unknown - driver assigned */
827 /* Set defaults for D0 and D3 states (always valid) */
828 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
829 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
830 device
->power
.states
[ACPI_STATE_D3
].flags
.valid
= 1;
831 device
->power
.states
[ACPI_STATE_D3
].power
= 0;
833 /* TBD: System wake support and resource requirements. */
835 device
->power
.state
= ACPI_STATE_UNKNOWN
;
840 static int acpi_bus_get_flags(struct acpi_device
*device
)
842 acpi_status status
= AE_OK
;
843 acpi_handle temp
= NULL
;
846 /* Presence of _STA indicates 'dynamic_status' */
847 status
= acpi_get_handle(device
->handle
, "_STA", &temp
);
848 if (ACPI_SUCCESS(status
))
849 device
->flags
.dynamic_status
= 1;
851 /* Presence of _CID indicates 'compatible_ids' */
852 status
= acpi_get_handle(device
->handle
, "_CID", &temp
);
853 if (ACPI_SUCCESS(status
))
854 device
->flags
.compatible_ids
= 1;
856 /* Presence of _RMV indicates 'removable' */
857 status
= acpi_get_handle(device
->handle
, "_RMV", &temp
);
858 if (ACPI_SUCCESS(status
))
859 device
->flags
.removable
= 1;
861 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
862 status
= acpi_get_handle(device
->handle
, "_EJD", &temp
);
863 if (ACPI_SUCCESS(status
))
864 device
->flags
.ejectable
= 1;
866 status
= acpi_get_handle(device
->handle
, "_EJ0", &temp
);
867 if (ACPI_SUCCESS(status
))
868 device
->flags
.ejectable
= 1;
871 /* Presence of _LCK indicates 'lockable' */
872 status
= acpi_get_handle(device
->handle
, "_LCK", &temp
);
873 if (ACPI_SUCCESS(status
))
874 device
->flags
.lockable
= 1;
876 /* Presence of _PS0|_PR0 indicates 'power manageable' */
877 status
= acpi_get_handle(device
->handle
, "_PS0", &temp
);
878 if (ACPI_FAILURE(status
))
879 status
= acpi_get_handle(device
->handle
, "_PR0", &temp
);
880 if (ACPI_SUCCESS(status
))
881 device
->flags
.power_manageable
= 1;
883 /* Presence of _PRW indicates wake capable */
884 status
= acpi_get_handle(device
->handle
, "_PRW", &temp
);
885 if (ACPI_SUCCESS(status
))
886 device
->flags
.wake_capable
= 1;
888 /* TBD: Performance management */
893 static void acpi_device_get_busid(struct acpi_device
*device
,
894 acpi_handle handle
, int type
)
896 char bus_id
[5] = { '?', 0 };
897 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
903 * The device's Bus ID is simply the object name.
904 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
907 case ACPI_BUS_TYPE_SYSTEM
:
908 strcpy(device
->pnp
.bus_id
, "ACPI");
910 case ACPI_BUS_TYPE_POWER_BUTTON
:
911 strcpy(device
->pnp
.bus_id
, "PWRF");
913 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
914 strcpy(device
->pnp
.bus_id
, "SLPF");
917 acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
);
918 /* Clean up trailing underscores (if any) */
919 for (i
= 3; i
> 1; i
--) {
920 if (bus_id
[i
] == '_')
925 strcpy(device
->pnp
.bus_id
, bus_id
);
931 acpi_video_bus_match(struct acpi_device
*device
)
938 /* Since there is no HID, CID for ACPI Video drivers, we have
939 * to check well known required nodes for each feature we support.
942 /* Does this device able to support video switching ? */
943 if (ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_DOD", &h_dummy
)) &&
944 ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_DOS", &h_dummy
)))
947 /* Does this device able to retrieve a video ROM ? */
948 if (ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_ROM", &h_dummy
)))
951 /* Does this device able to configure which video head to be POSTed ? */
952 if (ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_VPO", &h_dummy
)) &&
953 ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_GPD", &h_dummy
)) &&
954 ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_SPD", &h_dummy
)))
961 * acpi_bay_match - see if a device is an ejectable driver bay
963 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
964 * then we can safely call it an ejectable drive bay
966 static int acpi_bay_match(struct acpi_device
*device
){
972 handle
= device
->handle
;
974 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
975 if (ACPI_FAILURE(status
))
978 if ((ACPI_SUCCESS(acpi_get_handle(handle
, "_GTF", &tmp
))) ||
979 (ACPI_SUCCESS(acpi_get_handle(handle
, "_GTM", &tmp
))) ||
980 (ACPI_SUCCESS(acpi_get_handle(handle
, "_STM", &tmp
))) ||
981 (ACPI_SUCCESS(acpi_get_handle(handle
, "_SDD", &tmp
))))
984 if (acpi_get_parent(handle
, &phandle
))
987 if ((ACPI_SUCCESS(acpi_get_handle(phandle
, "_GTF", &tmp
))) ||
988 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_GTM", &tmp
))) ||
989 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_STM", &tmp
))) ||
990 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_SDD", &tmp
))))
997 * acpi_dock_match - see if a device has a _DCK method
999 static int acpi_dock_match(struct acpi_device
*device
)
1002 return acpi_get_handle(device
->handle
, "_DCK", &tmp
);
1005 static void acpi_device_set_id(struct acpi_device
*device
,
1006 struct acpi_device
*parent
, acpi_handle handle
,
1009 struct acpi_device_info
*info
;
1010 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1013 struct acpi_compatible_id_list
*cid_list
= NULL
;
1014 const char *cid_add
= NULL
;
1018 case ACPI_BUS_TYPE_DEVICE
:
1019 status
= acpi_get_object_info(handle
, &buffer
);
1020 if (ACPI_FAILURE(status
)) {
1021 printk(KERN_ERR PREFIX
"%s: Error reading device info\n", __func__
);
1025 info
= buffer
.pointer
;
1026 if (info
->valid
& ACPI_VALID_HID
)
1027 hid
= info
->hardware_id
.value
;
1028 if (info
->valid
& ACPI_VALID_UID
)
1029 uid
= info
->unique_id
.value
;
1030 if (info
->valid
& ACPI_VALID_CID
)
1031 cid_list
= &info
->compatibility_id
;
1032 if (info
->valid
& ACPI_VALID_ADR
) {
1033 device
->pnp
.bus_address
= info
->address
;
1034 device
->flags
.bus_address
= 1;
1037 /* If we have a video/bay/dock device, add our selfdefined
1038 HID to the CID list. Like that the video/bay/dock drivers
1039 will get autoloaded and the device might still match
1040 against another driver.
1042 if (ACPI_SUCCESS(acpi_video_bus_match(device
)))
1043 cid_add
= ACPI_VIDEO_HID
;
1044 else if (ACPI_SUCCESS(acpi_bay_match(device
)))
1045 cid_add
= ACPI_BAY_HID
;
1046 else if (ACPI_SUCCESS(acpi_dock_match(device
)))
1047 cid_add
= ACPI_DOCK_HID
;
1050 case ACPI_BUS_TYPE_POWER
:
1051 hid
= ACPI_POWER_HID
;
1053 case ACPI_BUS_TYPE_PROCESSOR
:
1054 hid
= ACPI_PROCESSOR_HID
;
1056 case ACPI_BUS_TYPE_SYSTEM
:
1057 hid
= ACPI_SYSTEM_HID
;
1059 case ACPI_BUS_TYPE_THERMAL
:
1060 hid
= ACPI_THERMAL_HID
;
1062 case ACPI_BUS_TYPE_POWER_BUTTON
:
1063 hid
= ACPI_BUTTON_HID_POWERF
;
1065 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1066 hid
= ACPI_BUTTON_HID_SLEEPF
;
1073 * Fix for the system root bus device -- the only root-level device.
1075 if (((acpi_handle
)parent
== ACPI_ROOT_OBJECT
) && (type
== ACPI_BUS_TYPE_DEVICE
)) {
1077 strcpy(device
->pnp
.device_name
, ACPI_BUS_DEVICE_NAME
);
1078 strcpy(device
->pnp
.device_class
, ACPI_BUS_CLASS
);
1082 strcpy(device
->pnp
.hardware_id
, hid
);
1083 device
->flags
.hardware_id
= 1;
1086 strcpy(device
->pnp
.unique_id
, uid
);
1087 device
->flags
.unique_id
= 1;
1089 if (cid_list
|| cid_add
) {
1090 struct acpi_compatible_id_list
*list
;
1095 size
= cid_list
->size
;
1096 } else if (cid_add
) {
1097 size
= sizeof(struct acpi_compatible_id_list
);
1098 cid_list
= ACPI_ALLOCATE_ZEROED((acpi_size
) size
);
1100 printk(KERN_ERR
"Memory allocation error\n");
1101 kfree(buffer
.pointer
);
1104 cid_list
->count
= 0;
1105 cid_list
->size
= size
;
1109 size
+= sizeof(struct acpi_compatible_id
);
1110 list
= kmalloc(size
, GFP_KERNEL
);
1114 memcpy(list
, cid_list
, cid_list
->size
);
1115 count
= cid_list
->count
;
1118 strncpy(list
->id
[count
].value
, cid_add
,
1119 ACPI_MAX_CID_LENGTH
);
1121 device
->flags
.compatible_ids
= 1;
1124 list
->count
= count
;
1125 device
->pnp
.cid_list
= list
;
1127 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1130 kfree(buffer
.pointer
);
1133 static int acpi_device_set_context(struct acpi_device
*device
, int type
)
1135 acpi_status status
= AE_OK
;
1140 * Attach this 'struct acpi_device' to the ACPI object. This makes
1141 * resolutions from handle->device very efficient. Note that we need
1142 * to be careful with fixed-feature devices as they all attach to the
1145 if (type
!= ACPI_BUS_TYPE_POWER_BUTTON
&&
1146 type
!= ACPI_BUS_TYPE_SLEEP_BUTTON
) {
1147 status
= acpi_attach_data(device
->handle
,
1148 acpi_bus_data_handler
, device
);
1150 if (ACPI_FAILURE(status
)) {
1151 printk(KERN_ERR PREFIX
"Error attaching device data\n");
1158 static int acpi_bus_remove(struct acpi_device
*dev
, int rmdevice
)
1163 dev
->removal_type
= ACPI_BUS_REMOVAL_EJECT
;
1164 device_release_driver(&dev
->dev
);
1170 * unbind _ADR-Based Devices when hot removal
1172 if (dev
->flags
.bus_address
) {
1173 if ((dev
->parent
) && (dev
->parent
->ops
.unbind
))
1174 dev
->parent
->ops
.unbind(dev
);
1176 acpi_device_unregister(dev
, ACPI_BUS_REMOVAL_EJECT
);
1182 acpi_is_child_device(struct acpi_device
*device
,
1183 int (*matcher
)(struct acpi_device
*))
1185 int result
= -ENODEV
;
1188 if (ACPI_SUCCESS(matcher(device
)))
1190 } while ((device
= device
->parent
));
1196 acpi_add_single_object(struct acpi_device
**child
,
1197 struct acpi_device
*parent
, acpi_handle handle
, int type
,
1198 struct acpi_bus_ops
*ops
)
1201 struct acpi_device
*device
= NULL
;
1207 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
1209 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1213 device
->handle
= handle
;
1214 device
->parent
= parent
;
1215 device
->bus_ops
= *ops
; /* workround for not call .start */
1218 acpi_device_get_busid(device
, handle
, type
);
1223 * Get prior to calling acpi_bus_get_status() so we know whether
1224 * or not _STA is present. Note that we only look for object
1225 * handles -- cannot evaluate objects until we know the device is
1226 * present and properly initialized.
1228 result
= acpi_bus_get_flags(device
);
1235 * See if the device is present. We always assume that non-Device
1236 * and non-Processor objects (e.g. thermal zones, power resources,
1237 * etc.) are present, functioning, etc. (at least when parent object
1238 * is present). Note that _STA has a different meaning for some
1239 * objects (e.g. power resources) so we need to be careful how we use
1243 case ACPI_BUS_TYPE_PROCESSOR
:
1244 case ACPI_BUS_TYPE_DEVICE
:
1245 result
= acpi_bus_get_status(device
);
1246 if (ACPI_FAILURE(result
)) {
1250 if (!device
->status
.present
) {
1251 /* Bay and dock should be handled even if absent */
1253 acpi_is_child_device(device
, acpi_bay_match
)) &&
1255 acpi_is_child_device(device
, acpi_dock_match
))) {
1262 STRUCT_TO_INT(device
->status
) =
1263 ACPI_STA_DEVICE_PRESENT
| ACPI_STA_DEVICE_ENABLED
|
1264 ACPI_STA_DEVICE_UI
| ACPI_STA_DEVICE_FUNCTIONING
;
1271 * TBD: Synch with Core's enumeration/initialization process.
1275 * Hardware ID, Unique ID, & Bus Address
1276 * -------------------------------------
1278 acpi_device_set_id(device
, parent
, handle
, type
);
1284 if (device
->flags
.power_manageable
) {
1285 result
= acpi_bus_get_power_flags(device
);
1291 * Wakeup device management
1292 *-----------------------
1294 if (device
->flags
.wake_capable
) {
1295 result
= acpi_bus_get_wakeup_device_flags(device
);
1301 * Performance Management
1302 * ----------------------
1304 if (device
->flags
.performance_manageable
) {
1305 result
= acpi_bus_get_perf_flags(device
);
1310 if ((result
= acpi_device_set_context(device
, type
)))
1313 result
= acpi_device_register(device
, parent
);
1316 * Bind _ADR-Based Devices when hot add
1318 if (device
->flags
.bus_address
) {
1319 if (device
->parent
&& device
->parent
->ops
.bind
)
1320 device
->parent
->ops
.bind(device
);
1327 kfree(device
->pnp
.cid_list
);
1334 static int acpi_bus_scan(struct acpi_device
*start
, struct acpi_bus_ops
*ops
)
1336 acpi_status status
= AE_OK
;
1337 struct acpi_device
*parent
= NULL
;
1338 struct acpi_device
*child
= NULL
;
1339 acpi_handle phandle
= NULL
;
1340 acpi_handle chandle
= NULL
;
1341 acpi_object_type type
= 0;
1349 phandle
= start
->handle
;
1352 * Parse through the ACPI namespace, identify all 'devices', and
1353 * create a new 'struct acpi_device' for each.
1355 while ((level
> 0) && parent
) {
1357 status
= acpi_get_next_object(ACPI_TYPE_ANY
, phandle
,
1361 * If this scope is exhausted then move our way back up.
1363 if (ACPI_FAILURE(status
)) {
1366 acpi_get_parent(phandle
, &phandle
);
1368 parent
= parent
->parent
;
1372 status
= acpi_get_type(chandle
, &type
);
1373 if (ACPI_FAILURE(status
))
1377 * If this is a scope object then parse it (depth-first).
1379 if (type
== ACPI_TYPE_LOCAL_SCOPE
) {
1387 * We're only interested in objects that we consider 'devices'.
1390 case ACPI_TYPE_DEVICE
:
1391 type
= ACPI_BUS_TYPE_DEVICE
;
1393 case ACPI_TYPE_PROCESSOR
:
1394 type
= ACPI_BUS_TYPE_PROCESSOR
;
1396 case ACPI_TYPE_THERMAL
:
1397 type
= ACPI_BUS_TYPE_THERMAL
;
1399 case ACPI_TYPE_POWER
:
1400 type
= ACPI_BUS_TYPE_POWER
;
1406 if (ops
->acpi_op_add
)
1407 status
= acpi_add_single_object(&child
, parent
,
1408 chandle
, type
, ops
);
1410 status
= acpi_bus_get_device(chandle
, &child
);
1412 if (ACPI_FAILURE(status
))
1415 if (ops
->acpi_op_start
&& !(ops
->acpi_op_add
)) {
1416 status
= acpi_start_single_object(child
);
1417 if (ACPI_FAILURE(status
))
1422 * If the device is present, enabled, and functioning then
1423 * parse its scope (depth-first). Note that we need to
1424 * represent absent devices to facilitate PnP notifications
1425 * -- but only the subtree head (not all of its children,
1426 * which will be enumerated when the parent is inserted).
1428 * TBD: Need notifications and other detection mechanisms
1429 * in place before we can fully implement this.
1431 if (child
->status
.present
) {
1432 status
= acpi_get_next_object(ACPI_TYPE_ANY
, chandle
,
1434 if (ACPI_SUCCESS(status
)) {
1447 acpi_bus_add(struct acpi_device
**child
,
1448 struct acpi_device
*parent
, acpi_handle handle
, int type
)
1451 struct acpi_bus_ops ops
;
1453 memset(&ops
, 0, sizeof(ops
));
1454 ops
.acpi_op_add
= 1;
1456 result
= acpi_add_single_object(child
, parent
, handle
, type
, &ops
);
1458 result
= acpi_bus_scan(*child
, &ops
);
1463 EXPORT_SYMBOL(acpi_bus_add
);
1465 int acpi_bus_start(struct acpi_device
*device
)
1468 struct acpi_bus_ops ops
;
1474 result
= acpi_start_single_object(device
);
1476 memset(&ops
, 0, sizeof(ops
));
1477 ops
.acpi_op_start
= 1;
1478 result
= acpi_bus_scan(device
, &ops
);
1483 EXPORT_SYMBOL(acpi_bus_start
);
1485 int acpi_bus_trim(struct acpi_device
*start
, int rmdevice
)
1488 struct acpi_device
*parent
, *child
;
1489 acpi_handle phandle
, chandle
;
1490 acpi_object_type type
;
1495 phandle
= start
->handle
;
1496 child
= chandle
= NULL
;
1498 while ((level
> 0) && parent
&& (!err
)) {
1499 status
= acpi_get_next_object(ACPI_TYPE_ANY
, phandle
,
1503 * If this scope is exhausted then move our way back up.
1505 if (ACPI_FAILURE(status
)) {
1508 acpi_get_parent(phandle
, &phandle
);
1510 parent
= parent
->parent
;
1513 err
= acpi_bus_remove(child
, rmdevice
);
1515 err
= acpi_bus_remove(child
, 1);
1520 status
= acpi_get_type(chandle
, &type
);
1521 if (ACPI_FAILURE(status
)) {
1525 * If there is a device corresponding to chandle then
1526 * parse it (depth-first).
1528 if (acpi_bus_get_device(chandle
, &child
) == 0) {
1538 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
1541 static int acpi_bus_scan_fixed(struct acpi_device
*root
)
1544 struct acpi_device
*device
= NULL
;
1545 struct acpi_bus_ops ops
;
1550 memset(&ops
, 0, sizeof(ops
));
1551 ops
.acpi_op_add
= 1;
1552 ops
.acpi_op_start
= 1;
1555 * Enumerate all fixed-feature devices.
1557 if ((acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
) == 0) {
1558 result
= acpi_add_single_object(&device
, acpi_root
,
1560 ACPI_BUS_TYPE_POWER_BUTTON
,
1564 if ((acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
) == 0) {
1565 result
= acpi_add_single_object(&device
, acpi_root
,
1567 ACPI_BUS_TYPE_SLEEP_BUTTON
,
1574 int __init
acpi_boot_ec_enable(void);
1576 static int __init
acpi_scan_init(void)
1579 struct acpi_bus_ops ops
;
1585 memset(&ops
, 0, sizeof(ops
));
1586 ops
.acpi_op_add
= 1;
1587 ops
.acpi_op_start
= 1;
1589 result
= bus_register(&acpi_bus_type
);
1591 /* We don't want to quit even if we failed to add suspend/resume */
1592 printk(KERN_ERR PREFIX
"Could not register bus type\n");
1596 * Create the root device in the bus's device tree
1598 result
= acpi_add_single_object(&acpi_root
, NULL
, ACPI_ROOT_OBJECT
,
1599 ACPI_BUS_TYPE_SYSTEM
, &ops
);
1604 * Enumerate devices in the ACPI namespace.
1606 result
= acpi_bus_scan_fixed(acpi_root
);
1608 /* EC region might be needed at bus_scan, so enable it now */
1609 acpi_boot_ec_enable();
1612 result
= acpi_bus_scan(acpi_root
, &ops
);
1615 acpi_device_unregister(acpi_root
, ACPI_BUS_REMOVAL_NORMAL
);
1621 subsys_initcall(acpi_scan_init
);