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>
10 #include <acpi/acpi_drivers.h>
11 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
13 #define _COMPONENT ACPI_BUS_COMPONENT
14 ACPI_MODULE_NAME("scan");
15 #define STRUCT_TO_INT(s) (*((int*)&s))
16 extern struct acpi_device *acpi_root;
18 #define ACPI_BUS_CLASS "system_bus"
19 #define ACPI_BUS_HID "LNXSYBUS"
20 #define ACPI_BUS_DEVICE_NAME "System Bus"
22 static LIST_HEAD(acpi_device_list);
23 static LIST_HEAD(acpi_bus_id_list);
24 DEFINE_SPINLOCK(acpi_device_lock);
25 LIST_HEAD(acpi_wakeup_device_list);
27 struct acpi_device_bus_id{
29 unsigned int instance_no;
30 struct list_head node;
34 * Creates hid/cid(s) string needed for modalias and uevent
35 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
36 * char *modalias: "acpi:IBM0001:ACPI0001"
38 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44 if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
47 len = snprintf(modalias, size, "acpi:");
50 if (acpi_dev->flags.hardware_id) {
51 count = snprintf(&modalias[len], size, "%s:",
52 acpi_dev->pnp.hardware_id);
53 if (count < 0 || count >= size)
59 if (acpi_dev->flags.compatible_ids) {
60 struct acpi_compatible_id_list *cid_list;
63 cid_list = acpi_dev->pnp.cid_list;
64 for (i = 0; i < cid_list->count; i++) {
65 count = snprintf(&modalias[len], size, "%s:",
66 cid_list->id[i].value);
67 if (count < 0 || count >= size) {
68 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
69 acpi_dev->pnp.device_name, i);
82 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
83 struct acpi_device *acpi_dev = to_acpi_device(dev);
86 /* Device has no HID and no CID or string is >1024 */
87 len = create_modalias(acpi_dev, buf, 1024);
93 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
95 static int acpi_eject_operation(acpi_handle handle, int lockable)
97 struct acpi_object_list arg_list;
98 union acpi_object arg;
99 acpi_status status = AE_OK;
102 * TBD: evaluate _PS3?
107 arg_list.pointer = &arg;
108 arg.type = ACPI_TYPE_INTEGER;
109 arg.integer.value = 0;
110 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
114 arg_list.pointer = &arg;
115 arg.type = ACPI_TYPE_INTEGER;
116 arg.integer.value = 1;
122 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
123 if (ACPI_FAILURE(status)) {
131 acpi_eject_store(struct device *d, struct device_attribute *attr,
132 const char *buf, size_t count)
139 acpi_object_type type = 0;
140 struct acpi_device *acpi_device = to_acpi_device(d);
142 if ((!count) || (buf[0] != '1')) {
146 if (acpi_device->driver == NULL) {
151 status = acpi_get_type(acpi_device->handle, &type);
152 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
157 islockable = acpi_device->flags.lockable;
158 handle = acpi_device->handle;
160 result = acpi_bus_trim(acpi_device, 1);
163 result = acpi_eject_operation(handle, islockable);
172 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
175 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
176 struct acpi_device *acpi_dev = to_acpi_device(dev);
178 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
180 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
183 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
184 struct acpi_device *acpi_dev = to_acpi_device(dev);
185 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
188 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
192 result = sprintf(buf, "%s\n", (char*)path.pointer);
197 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
199 static int acpi_device_setup_files(struct acpi_device *dev)
206 * Devices gotten from FADT don't have a "path" attribute
209 result = device_create_file(&dev->dev, &dev_attr_path);
214 if(dev->flags.hardware_id) {
215 result = device_create_file(&dev->dev, &dev_attr_hid);
220 if (dev->flags.hardware_id || dev->flags.compatible_ids){
221 result = device_create_file(&dev->dev, &dev_attr_modalias);
227 * If device has _EJ0, 'eject' file is created that is used to trigger
228 * hot-removal function from userland.
230 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
231 if (ACPI_SUCCESS(status))
232 result = device_create_file(&dev->dev, &dev_attr_eject);
237 static void acpi_device_remove_files(struct acpi_device *dev)
243 * If device has _EJ0, 'eject' file is created that is used to trigger
244 * hot-removal function from userland.
246 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
247 if (ACPI_SUCCESS(status))
248 device_remove_file(&dev->dev, &dev_attr_eject);
250 if (dev->flags.hardware_id || dev->flags.compatible_ids)
251 device_remove_file(&dev->dev, &dev_attr_modalias);
253 if(dev->flags.hardware_id)
254 device_remove_file(&dev->dev, &dev_attr_hid);
256 device_remove_file(&dev->dev, &dev_attr_path);
258 /* --------------------------------------------------------------------------
260 -------------------------------------------------------------------------- */
262 int acpi_match_device_ids(struct acpi_device *device,
263 const struct acpi_device_id *ids)
265 const struct acpi_device_id *id;
267 if (device->flags.hardware_id) {
268 for (id = ids; id->id[0]; id++) {
269 if (!strcmp((char*)id->id, device->pnp.hardware_id))
274 if (device->flags.compatible_ids) {
275 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
278 for (id = ids; id->id[0]; id++) {
279 /* compare multiple _CID entries against driver ids */
280 for (i = 0; i < cid_list->count; i++) {
281 if (!strcmp((char*)id->id,
282 cid_list->id[i].value))
290 EXPORT_SYMBOL(acpi_match_device_ids);
292 static void acpi_device_release(struct device *dev)
294 struct acpi_device *acpi_dev = to_acpi_device(dev);
296 kfree(acpi_dev->pnp.cid_list);
300 static int acpi_device_suspend(struct device *dev, pm_message_t state)
302 struct acpi_device *acpi_dev = to_acpi_device(dev);
303 struct acpi_driver *acpi_drv = acpi_dev->driver;
305 if (acpi_drv && acpi_drv->ops.suspend)
306 return acpi_drv->ops.suspend(acpi_dev, state);
310 static int acpi_device_resume(struct device *dev)
312 struct acpi_device *acpi_dev = to_acpi_device(dev);
313 struct acpi_driver *acpi_drv = acpi_dev->driver;
315 if (acpi_drv && acpi_drv->ops.resume)
316 return acpi_drv->ops.resume(acpi_dev);
320 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
322 struct acpi_device *acpi_dev = to_acpi_device(dev);
323 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
325 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
328 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
330 struct acpi_device *acpi_dev = to_acpi_device(dev);
333 if (add_uevent_var(env, "MODALIAS="))
335 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
336 sizeof(env->buf) - env->buflen);
337 if (len >= (sizeof(env->buf) - env->buflen))
343 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
344 static int acpi_start_single_object(struct acpi_device *);
345 static int acpi_device_probe(struct device * dev)
347 struct acpi_device *acpi_dev = to_acpi_device(dev);
348 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
351 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
353 if (acpi_dev->bus_ops.acpi_op_start)
354 acpi_start_single_object(acpi_dev);
355 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
356 "Found driver [%s] for device [%s]\n",
357 acpi_drv->name, acpi_dev->pnp.bus_id));
363 static int acpi_device_remove(struct device * dev)
365 struct acpi_device *acpi_dev = to_acpi_device(dev);
366 struct acpi_driver *acpi_drv = acpi_dev->driver;
369 if (acpi_drv->ops.stop)
370 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
371 if (acpi_drv->ops.remove)
372 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
374 acpi_dev->driver = NULL;
375 acpi_driver_data(dev) = NULL;
381 static void acpi_device_shutdown(struct device *dev)
383 struct acpi_device *acpi_dev = to_acpi_device(dev);
384 struct acpi_driver *acpi_drv = acpi_dev->driver;
386 if (acpi_drv && acpi_drv->ops.shutdown)
387 acpi_drv->ops.shutdown(acpi_dev);
392 struct bus_type acpi_bus_type = {
394 .suspend = acpi_device_suspend,
395 .resume = acpi_device_resume,
396 .shutdown = acpi_device_shutdown,
397 .match = acpi_bus_match,
398 .probe = acpi_device_probe,
399 .remove = acpi_device_remove,
400 .uevent = acpi_device_uevent,
403 static int acpi_device_register(struct acpi_device *device,
404 struct acpi_device *parent)
407 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
412 * Link this device to its parent and siblings.
414 INIT_LIST_HEAD(&device->children);
415 INIT_LIST_HEAD(&device->node);
416 INIT_LIST_HEAD(&device->g_list);
417 INIT_LIST_HEAD(&device->wakeup_list);
419 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
421 printk(KERN_ERR PREFIX "Memory allocation error\n");
425 spin_lock(&acpi_device_lock);
427 * Find suitable bus_id and instance number in acpi_bus_id_list
428 * If failed, create one and link it into acpi_bus_id_list
430 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
431 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
432 acpi_device_bus_id->instance_no ++;
439 acpi_device_bus_id = new_bus_id;
440 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
441 acpi_device_bus_id->instance_no = 0;
442 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
444 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
446 if (device->parent) {
447 list_add_tail(&device->node, &device->parent->children);
448 list_add_tail(&device->g_list, &device->parent->g_list);
450 list_add_tail(&device->g_list, &acpi_device_list);
451 if (device->wakeup.flags.valid)
452 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
453 spin_unlock(&acpi_device_lock);
456 device->dev.parent = &parent->dev;
457 device->dev.bus = &acpi_bus_type;
458 device_initialize(&device->dev);
459 device->dev.release = &acpi_device_release;
460 result = device_add(&device->dev);
462 printk(KERN_ERR PREFIX "Error adding device %s", device->dev.bus_id);
466 result = acpi_device_setup_files(device);
468 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
470 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
473 spin_lock(&acpi_device_lock);
474 if (device->parent) {
475 list_del(&device->node);
476 list_del(&device->g_list);
478 list_del(&device->g_list);
479 list_del(&device->wakeup_list);
480 spin_unlock(&acpi_device_lock);
484 static void acpi_device_unregister(struct acpi_device *device, int type)
486 spin_lock(&acpi_device_lock);
487 if (device->parent) {
488 list_del(&device->node);
489 list_del(&device->g_list);
491 list_del(&device->g_list);
493 list_del(&device->wakeup_list);
494 spin_unlock(&acpi_device_lock);
496 acpi_detach_data(device->handle, acpi_bus_data_handler);
498 acpi_device_remove_files(device);
499 device_unregister(&device->dev);
502 /* --------------------------------------------------------------------------
504 -------------------------------------------------------------------------- */
506 * acpi_bus_driver_init - add a device to a driver
507 * @device: the device to add and initialize
508 * @driver: driver for the device
510 * Used to initialize a device via its device driver. Called whenever a
511 * driver is bound to a device. Invokes the driver's add() ops.
514 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
519 if (!device || !driver)
522 if (!driver->ops.add)
525 result = driver->ops.add(device);
527 device->driver = NULL;
528 acpi_driver_data(device) = NULL;
532 device->driver = driver;
535 * TBD - Configuration Management: Assign resources to device based
536 * upon possible configuration and currently allocated resources.
539 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
540 "Driver successfully bound to device\n"));
544 static int acpi_start_single_object(struct acpi_device *device)
547 struct acpi_driver *driver;
550 if (!(driver = device->driver))
553 if (driver->ops.start) {
554 result = driver->ops.start(device);
555 if (result && driver->ops.remove)
556 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
563 * acpi_bus_register_driver - register a driver with the ACPI bus
564 * @driver: driver being registered
566 * Registers a driver with the ACPI bus. Searches the namespace for all
567 * devices that match the driver's criteria and binds. Returns zero for
568 * success or a negative error status for failure.
570 int acpi_bus_register_driver(struct acpi_driver *driver)
576 driver->drv.name = driver->name;
577 driver->drv.bus = &acpi_bus_type;
578 driver->drv.owner = driver->owner;
580 ret = driver_register(&driver->drv);
584 EXPORT_SYMBOL(acpi_bus_register_driver);
587 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
588 * @driver: driver to unregister
590 * Unregisters a driver with the ACPI bus. Searches the namespace for all
591 * devices that match the driver's criteria and unbinds.
593 void acpi_bus_unregister_driver(struct acpi_driver *driver)
595 driver_unregister(&driver->drv);
598 EXPORT_SYMBOL(acpi_bus_unregister_driver);
600 /* --------------------------------------------------------------------------
602 -------------------------------------------------------------------------- */
604 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
608 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
609 union acpi_object *obj;
611 status = acpi_get_handle(handle, "_EJD", &tmp);
612 if (ACPI_FAILURE(status))
615 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
616 if (ACPI_SUCCESS(status)) {
617 obj = buffer.pointer;
618 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
620 kfree(buffer.pointer);
624 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
626 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
634 static int acpi_bus_get_perf_flags(struct acpi_device *device)
636 device->performance.state = ACPI_STATE_UNKNOWN;
641 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
642 union acpi_object *package)
645 union acpi_object *element = NULL;
647 if (!device || !package || (package->package.count < 2))
648 return AE_BAD_PARAMETER;
650 element = &(package->package.elements[0]);
652 return AE_BAD_PARAMETER;
653 if (element->type == ACPI_TYPE_PACKAGE) {
654 if ((element->package.count < 2) ||
655 (element->package.elements[0].type !=
656 ACPI_TYPE_LOCAL_REFERENCE)
657 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
659 device->wakeup.gpe_device =
660 element->package.elements[0].reference.handle;
661 device->wakeup.gpe_number =
662 (u32) element->package.elements[1].integer.value;
663 } else if (element->type == ACPI_TYPE_INTEGER) {
664 device->wakeup.gpe_number = element->integer.value;
668 element = &(package->package.elements[1]);
669 if (element->type != ACPI_TYPE_INTEGER) {
672 device->wakeup.sleep_state = element->integer.value;
674 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
677 device->wakeup.resources.count = package->package.count - 2;
678 for (i = 0; i < device->wakeup.resources.count; i++) {
679 element = &(package->package.elements[i + 2]);
680 if (element->type != ACPI_TYPE_ANY) {
684 device->wakeup.resources.handles[i] = element->reference.handle;
690 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
692 acpi_status status = 0;
693 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
694 union acpi_object *package = NULL;
695 union acpi_object in_arg[3];
696 struct acpi_object_list arg_list = { 3, in_arg };
697 acpi_status psw_status = AE_OK;
699 struct acpi_device_id button_device_ids[] = {
707 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
708 if (ACPI_FAILURE(status)) {
709 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
713 package = (union acpi_object *)buffer.pointer;
714 status = acpi_bus_extract_wakeup_device_power_package(device, package);
715 if (ACPI_FAILURE(status)) {
716 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
720 kfree(buffer.pointer);
722 device->wakeup.flags.valid = 1;
723 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
724 * system for the ACPI device with the _PRW object.
725 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
726 * So it is necessary to call _DSW object first. Only when it is not
727 * present will the _PSW object used.
730 * Three agruments are needed for the _DSW object.
731 * Argument 0: enable/disable the wake capabilities
732 * When _DSW object is called to disable the wake capabilities, maybe
733 * the first argument is filled. The value of the other two agruments
736 in_arg[0].type = ACPI_TYPE_INTEGER;
737 in_arg[0].integer.value = 0;
738 in_arg[1].type = ACPI_TYPE_INTEGER;
739 in_arg[1].integer.value = 0;
740 in_arg[2].type = ACPI_TYPE_INTEGER;
741 in_arg[2].integer.value = 0;
742 psw_status = acpi_evaluate_object(device->handle, "_DSW",
744 if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
745 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in evaluate _DSW\n"));
747 * When the _DSW object is not present, OSPM will call _PSW object.
749 if (psw_status == AE_NOT_FOUND) {
751 * Only one agruments is required for the _PSW object.
752 * agrument 0: enable/disable the wake capabilities
755 in_arg[0].integer.value = 0;
756 psw_status = acpi_evaluate_object(device->handle, "_PSW",
758 if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
759 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in "
762 /* Power button, Lid switch always enable wakeup */
763 if (!acpi_match_device_ids(device, button_device_ids))
764 device->wakeup.flags.run_wake = 1;
767 if (ACPI_FAILURE(status))
768 device->flags.wake_capable = 0;
772 static int acpi_bus_get_power_flags(struct acpi_device *device)
774 acpi_status status = 0;
775 acpi_handle handle = NULL;
780 * Power Management Flags
782 status = acpi_get_handle(device->handle, "_PSC", &handle);
783 if (ACPI_SUCCESS(status))
784 device->power.flags.explicit_get = 1;
785 status = acpi_get_handle(device->handle, "_IRC", &handle);
786 if (ACPI_SUCCESS(status))
787 device->power.flags.inrush_current = 1;
790 * Enumerate supported power management states
792 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
793 struct acpi_device_power_state *ps = &device->power.states[i];
794 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
796 /* Evaluate "_PRx" to se if power resources are referenced */
797 acpi_evaluate_reference(device->handle, object_name, NULL,
799 if (ps->resources.count) {
800 device->power.flags.power_resources = 1;
804 /* Evaluate "_PSx" to see if we can do explicit sets */
805 object_name[2] = 'S';
806 status = acpi_get_handle(device->handle, object_name, &handle);
807 if (ACPI_SUCCESS(status)) {
808 ps->flags.explicit_set = 1;
812 /* State is valid if we have some power control */
813 if (ps->resources.count || ps->flags.explicit_set)
816 ps->power = -1; /* Unknown - driver assigned */
817 ps->latency = -1; /* Unknown - driver assigned */
820 /* Set defaults for D0 and D3 states (always valid) */
821 device->power.states[ACPI_STATE_D0].flags.valid = 1;
822 device->power.states[ACPI_STATE_D0].power = 100;
823 device->power.states[ACPI_STATE_D3].flags.valid = 1;
824 device->power.states[ACPI_STATE_D3].power = 0;
826 /* TBD: System wake support and resource requirements. */
828 device->power.state = ACPI_STATE_UNKNOWN;
833 static int acpi_bus_get_flags(struct acpi_device *device)
835 acpi_status status = AE_OK;
836 acpi_handle temp = NULL;
839 /* Presence of _STA indicates 'dynamic_status' */
840 status = acpi_get_handle(device->handle, "_STA", &temp);
841 if (ACPI_SUCCESS(status))
842 device->flags.dynamic_status = 1;
844 /* Presence of _CID indicates 'compatible_ids' */
845 status = acpi_get_handle(device->handle, "_CID", &temp);
846 if (ACPI_SUCCESS(status))
847 device->flags.compatible_ids = 1;
849 /* Presence of _RMV indicates 'removable' */
850 status = acpi_get_handle(device->handle, "_RMV", &temp);
851 if (ACPI_SUCCESS(status))
852 device->flags.removable = 1;
854 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
855 status = acpi_get_handle(device->handle, "_EJD", &temp);
856 if (ACPI_SUCCESS(status))
857 device->flags.ejectable = 1;
859 status = acpi_get_handle(device->handle, "_EJ0", &temp);
860 if (ACPI_SUCCESS(status))
861 device->flags.ejectable = 1;
864 /* Presence of _LCK indicates 'lockable' */
865 status = acpi_get_handle(device->handle, "_LCK", &temp);
866 if (ACPI_SUCCESS(status))
867 device->flags.lockable = 1;
869 /* Presence of _PS0|_PR0 indicates 'power manageable' */
870 status = acpi_get_handle(device->handle, "_PS0", &temp);
871 if (ACPI_FAILURE(status))
872 status = acpi_get_handle(device->handle, "_PR0", &temp);
873 if (ACPI_SUCCESS(status))
874 device->flags.power_manageable = 1;
876 /* Presence of _PRW indicates wake capable */
877 status = acpi_get_handle(device->handle, "_PRW", &temp);
878 if (ACPI_SUCCESS(status))
879 device->flags.wake_capable = 1;
881 /* TBD: Performance management */
886 static void acpi_device_get_busid(struct acpi_device *device,
887 acpi_handle handle, int type)
889 char bus_id[5] = { '?', 0 };
890 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
896 * The device's Bus ID is simply the object name.
897 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
900 case ACPI_BUS_TYPE_SYSTEM:
901 strcpy(device->pnp.bus_id, "ACPI");
903 case ACPI_BUS_TYPE_POWER_BUTTON:
904 strcpy(device->pnp.bus_id, "PWRF");
906 case ACPI_BUS_TYPE_SLEEP_BUTTON:
907 strcpy(device->pnp.bus_id, "SLPF");
910 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
911 /* Clean up trailing underscores (if any) */
912 for (i = 3; i > 1; i--) {
913 if (bus_id[i] == '_')
918 strcpy(device->pnp.bus_id, bus_id);
924 acpi_video_bus_match(struct acpi_device *device)
926 acpi_handle h_dummy1;
927 acpi_handle h_dummy2;
928 acpi_handle h_dummy3;
934 /* Since there is no HID, CID for ACPI Video drivers, we have
935 * to check well known required nodes for each feature we support.
938 /* Does this device able to support video switching ? */
939 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
940 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
943 /* Does this device able to retrieve a video ROM ? */
944 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
947 /* Does this device able to configure which video head to be POSTed ? */
948 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
949 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
950 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
957 * acpi_bay_match - see if a device is an ejectable driver bay
959 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
960 * then we can safely call it an ejectable drive bay
962 static int acpi_bay_match(struct acpi_device *device){
968 handle = device->handle;
970 status = acpi_get_handle(handle, "_EJ0", &tmp);
971 if (ACPI_FAILURE(status))
974 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
975 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
976 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
977 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
980 if (acpi_get_parent(handle, &phandle))
983 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
984 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
985 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
986 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
993 * acpi_dock_match - see if a device has a _DCK method
995 static int acpi_dock_match(struct acpi_device *device)
998 return acpi_get_handle(device->handle, "_DCK", &tmp);
1001 static void acpi_device_set_id(struct acpi_device *device,
1002 struct acpi_device *parent, acpi_handle handle,
1005 struct acpi_device_info *info;
1006 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1009 struct acpi_compatible_id_list *cid_list = NULL;
1010 const char *cid_add = NULL;
1014 case ACPI_BUS_TYPE_DEVICE:
1015 status = acpi_get_object_info(handle, &buffer);
1016 if (ACPI_FAILURE(status)) {
1017 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1021 info = buffer.pointer;
1022 if (info->valid & ACPI_VALID_HID)
1023 hid = info->hardware_id.value;
1024 if (info->valid & ACPI_VALID_UID)
1025 uid = info->unique_id.value;
1026 if (info->valid & ACPI_VALID_CID)
1027 cid_list = &info->compatibility_id;
1028 if (info->valid & ACPI_VALID_ADR) {
1029 device->pnp.bus_address = info->address;
1030 device->flags.bus_address = 1;
1033 /* If we have a video/bay/dock device, add our selfdefined
1034 HID to the CID list. Like that the video/bay/dock drivers
1035 will get autoloaded and the device might still match
1036 against another driver.
1038 if (ACPI_SUCCESS(acpi_video_bus_match(device)))
1039 cid_add = ACPI_VIDEO_HID;
1040 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1041 cid_add = ACPI_BAY_HID;
1042 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1043 cid_add = ACPI_DOCK_HID;
1046 case ACPI_BUS_TYPE_POWER:
1047 hid = ACPI_POWER_HID;
1049 case ACPI_BUS_TYPE_PROCESSOR:
1050 hid = ACPI_PROCESSOR_HID;
1052 case ACPI_BUS_TYPE_SYSTEM:
1053 hid = ACPI_SYSTEM_HID;
1055 case ACPI_BUS_TYPE_THERMAL:
1056 hid = ACPI_THERMAL_HID;
1058 case ACPI_BUS_TYPE_POWER_BUTTON:
1059 hid = ACPI_BUTTON_HID_POWERF;
1061 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1062 hid = ACPI_BUTTON_HID_SLEEPF;
1069 * Fix for the system root bus device -- the only root-level device.
1071 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1073 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1074 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1078 strcpy(device->pnp.hardware_id, hid);
1079 device->flags.hardware_id = 1;
1082 strcpy(device->pnp.unique_id, uid);
1083 device->flags.unique_id = 1;
1085 if (cid_list || cid_add) {
1086 struct acpi_compatible_id_list *list;
1091 size = cid_list->size;
1092 } else if (cid_add) {
1093 size = sizeof(struct acpi_compatible_id_list);
1094 cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
1096 printk(KERN_ERR "Memory allocation error\n");
1097 kfree(buffer.pointer);
1100 cid_list->count = 0;
1101 cid_list->size = size;
1105 size += sizeof(struct acpi_compatible_id);
1106 list = kmalloc(size, GFP_KERNEL);
1110 memcpy(list, cid_list, cid_list->size);
1111 count = cid_list->count;
1114 strncpy(list->id[count].value, cid_add,
1115 ACPI_MAX_CID_LENGTH);
1117 device->flags.compatible_ids = 1;
1120 list->count = count;
1121 device->pnp.cid_list = list;
1123 printk(KERN_ERR PREFIX "Memory allocation error\n");
1126 kfree(buffer.pointer);
1129 static int acpi_device_set_context(struct acpi_device *device, int type)
1131 acpi_status status = AE_OK;
1136 * Attach this 'struct acpi_device' to the ACPI object. This makes
1137 * resolutions from handle->device very efficient. Note that we need
1138 * to be careful with fixed-feature devices as they all attach to the
1141 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1142 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1143 status = acpi_attach_data(device->handle,
1144 acpi_bus_data_handler, device);
1146 if (ACPI_FAILURE(status)) {
1147 printk(KERN_ERR PREFIX "Error attaching device data\n");
1154 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1159 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1160 device_release_driver(&dev->dev);
1166 * unbind _ADR-Based Devices when hot removal
1168 if (dev->flags.bus_address) {
1169 if ((dev->parent) && (dev->parent->ops.unbind))
1170 dev->parent->ops.unbind(dev);
1172 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1178 acpi_is_child_device(struct acpi_device *device,
1179 int (*matcher)(struct acpi_device *))
1181 int result = -ENODEV;
1184 if (ACPI_SUCCESS(matcher(device)))
1186 } while ((device = device->parent));
1192 acpi_add_single_object(struct acpi_device **child,
1193 struct acpi_device *parent, acpi_handle handle, int type,
1194 struct acpi_bus_ops *ops)
1197 struct acpi_device *device = NULL;
1203 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1205 printk(KERN_ERR PREFIX "Memory allocation error\n");
1209 device->handle = handle;
1210 device->parent = parent;
1211 device->bus_ops = *ops; /* workround for not call .start */
1214 acpi_device_get_busid(device, handle, type);
1219 * Get prior to calling acpi_bus_get_status() so we know whether
1220 * or not _STA is present. Note that we only look for object
1221 * handles -- cannot evaluate objects until we know the device is
1222 * present and properly initialized.
1224 result = acpi_bus_get_flags(device);
1231 * See if the device is present. We always assume that non-Device
1232 * and non-Processor objects (e.g. thermal zones, power resources,
1233 * etc.) are present, functioning, etc. (at least when parent object
1234 * is present). Note that _STA has a different meaning for some
1235 * objects (e.g. power resources) so we need to be careful how we use
1239 case ACPI_BUS_TYPE_PROCESSOR:
1240 case ACPI_BUS_TYPE_DEVICE:
1241 result = acpi_bus_get_status(device);
1242 if (ACPI_FAILURE(result)) {
1246 if (!device->status.present) {
1247 /* Bay and dock should be handled even if absent */
1249 acpi_is_child_device(device, acpi_bay_match)) &&
1251 acpi_is_child_device(device, acpi_dock_match))) {
1258 STRUCT_TO_INT(device->status) =
1259 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1260 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
1267 * TBD: Synch with Core's enumeration/initialization process.
1271 * Hardware ID, Unique ID, & Bus Address
1272 * -------------------------------------
1274 acpi_device_set_id(device, parent, handle, type);
1280 if (device->flags.power_manageable) {
1281 result = acpi_bus_get_power_flags(device);
1287 * Wakeup device management
1288 *-----------------------
1290 if (device->flags.wake_capable) {
1291 result = acpi_bus_get_wakeup_device_flags(device);
1297 * Performance Management
1298 * ----------------------
1300 if (device->flags.performance_manageable) {
1301 result = acpi_bus_get_perf_flags(device);
1306 if ((result = acpi_device_set_context(device, type)))
1309 result = acpi_device_register(device, parent);
1312 * Bind _ADR-Based Devices when hot add
1314 if (device->flags.bus_address) {
1315 if (device->parent && device->parent->ops.bind)
1316 device->parent->ops.bind(device);
1323 kfree(device->pnp.cid_list);
1330 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1332 acpi_status status = AE_OK;
1333 struct acpi_device *parent = NULL;
1334 struct acpi_device *child = NULL;
1335 acpi_handle phandle = NULL;
1336 acpi_handle chandle = NULL;
1337 acpi_object_type type = 0;
1345 phandle = start->handle;
1348 * Parse through the ACPI namespace, identify all 'devices', and
1349 * create a new 'struct acpi_device' for each.
1351 while ((level > 0) && parent) {
1353 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1357 * If this scope is exhausted then move our way back up.
1359 if (ACPI_FAILURE(status)) {
1362 acpi_get_parent(phandle, &phandle);
1364 parent = parent->parent;
1368 status = acpi_get_type(chandle, &type);
1369 if (ACPI_FAILURE(status))
1373 * If this is a scope object then parse it (depth-first).
1375 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1383 * We're only interested in objects that we consider 'devices'.
1386 case ACPI_TYPE_DEVICE:
1387 type = ACPI_BUS_TYPE_DEVICE;
1389 case ACPI_TYPE_PROCESSOR:
1390 type = ACPI_BUS_TYPE_PROCESSOR;
1392 case ACPI_TYPE_THERMAL:
1393 type = ACPI_BUS_TYPE_THERMAL;
1395 case ACPI_TYPE_POWER:
1396 type = ACPI_BUS_TYPE_POWER;
1402 if (ops->acpi_op_add)
1403 status = acpi_add_single_object(&child, parent,
1404 chandle, type, ops);
1406 status = acpi_bus_get_device(chandle, &child);
1408 if (ACPI_FAILURE(status))
1411 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1412 status = acpi_start_single_object(child);
1413 if (ACPI_FAILURE(status))
1418 * If the device is present, enabled, and functioning then
1419 * parse its scope (depth-first). Note that we need to
1420 * represent absent devices to facilitate PnP notifications
1421 * -- but only the subtree head (not all of its children,
1422 * which will be enumerated when the parent is inserted).
1424 * TBD: Need notifications and other detection mechanisms
1425 * in place before we can fully implement this.
1427 if (child->status.present) {
1428 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1430 if (ACPI_SUCCESS(status)) {
1443 acpi_bus_add(struct acpi_device **child,
1444 struct acpi_device *parent, acpi_handle handle, int type)
1447 struct acpi_bus_ops ops;
1449 memset(&ops, 0, sizeof(ops));
1450 ops.acpi_op_add = 1;
1452 result = acpi_add_single_object(child, parent, handle, type, &ops);
1454 result = acpi_bus_scan(*child, &ops);
1459 EXPORT_SYMBOL(acpi_bus_add);
1461 int acpi_bus_start(struct acpi_device *device)
1464 struct acpi_bus_ops ops;
1470 result = acpi_start_single_object(device);
1472 memset(&ops, 0, sizeof(ops));
1473 ops.acpi_op_start = 1;
1474 result = acpi_bus_scan(device, &ops);
1479 EXPORT_SYMBOL(acpi_bus_start);
1481 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1484 struct acpi_device *parent, *child;
1485 acpi_handle phandle, chandle;
1486 acpi_object_type type;
1491 phandle = start->handle;
1492 child = chandle = NULL;
1494 while ((level > 0) && parent && (!err)) {
1495 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1499 * If this scope is exhausted then move our way back up.
1501 if (ACPI_FAILURE(status)) {
1504 acpi_get_parent(phandle, &phandle);
1506 parent = parent->parent;
1509 err = acpi_bus_remove(child, rmdevice);
1511 err = acpi_bus_remove(child, 1);
1516 status = acpi_get_type(chandle, &type);
1517 if (ACPI_FAILURE(status)) {
1521 * If there is a device corresponding to chandle then
1522 * parse it (depth-first).
1524 if (acpi_bus_get_device(chandle, &child) == 0) {
1534 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1537 static int acpi_bus_scan_fixed(struct acpi_device *root)
1540 struct acpi_device *device = NULL;
1541 struct acpi_bus_ops ops;
1546 memset(&ops, 0, sizeof(ops));
1547 ops.acpi_op_add = 1;
1548 ops.acpi_op_start = 1;
1551 * Enumerate all fixed-feature devices.
1553 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1554 result = acpi_add_single_object(&device, acpi_root,
1556 ACPI_BUS_TYPE_POWER_BUTTON,
1560 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1561 result = acpi_add_single_object(&device, acpi_root,
1563 ACPI_BUS_TYPE_SLEEP_BUTTON,
1570 int __init acpi_boot_ec_enable(void);
1572 static int __init acpi_scan_init(void)
1575 struct acpi_bus_ops ops;
1581 memset(&ops, 0, sizeof(ops));
1582 ops.acpi_op_add = 1;
1583 ops.acpi_op_start = 1;
1585 result = bus_register(&acpi_bus_type);
1587 /* We don't want to quit even if we failed to add suspend/resume */
1588 printk(KERN_ERR PREFIX "Could not register bus type\n");
1592 * Create the root device in the bus's device tree
1594 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1595 ACPI_BUS_TYPE_SYSTEM, &ops);
1600 * Enumerate devices in the ACPI namespace.
1602 result = acpi_bus_scan_fixed(acpi_root);
1604 /* EC region might be needed at bus_scan, so enable it now */
1605 acpi_boot_ec_enable();
1608 result = acpi_bus_scan(acpi_root, &ops);
1611 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1617 subsys_initcall(acpi_scan_init);